├── src ├── assets │ └── .gitkeep ├── app │ ├── app.component.scss │ ├── page │ │ ├── page │ │ │ ├── page.component.scss │ │ │ ├── page.component.html │ │ │ ├── page.component.spec.ts │ │ │ └── page.component.ts │ │ ├── page.module.ts │ │ └── page-routing.module.ts │ ├── not-found │ │ ├── not-found.component.scss │ │ ├── not-found.component.html │ │ ├── not-found.component.ts │ │ └── not-found.component.spec.ts │ ├── core │ │ ├── core.module.ts │ │ ├── cosmic.service.spec.ts │ │ ├── cosmic.service.ts │ │ └── http-error.interceptor.ts │ ├── app.component.ts │ ├── app-routing.module.ts │ ├── app.component.html │ ├── app.module.ts │ └── app.component.spec.ts ├── favicon.ico ├── tsconfig.app.json ├── environments │ ├── environment.ts │ └── environment.prod.ts ├── tsconfig.spec.json ├── index.html ├── tslint.json ├── main.ts ├── browserslist ├── styles.scss ├── test.ts ├── karma.conf.js └── polyfills.ts ├── scripts ├── object-type.json ├── objects.json └── import.js ├── e2e ├── src │ ├── app.po.ts │ └── app.e2e-spec.ts ├── tsconfig.e2e.json └── protractor.conf.js ├── prettier.config.js ├── tsconfig.json ├── README.md ├── server.js ├── .gitignore ├── set-env.ts ├── package.json ├── tslint.json └── angular.json /src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/page/page/page.component.scss: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/not-found/not-found.component.scss: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmicjs/angular-starter/master/src/favicon.ico -------------------------------------------------------------------------------- /scripts/object-type.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "Pages", 3 | "slug": "pages", 4 | "singular": "page" 5 | } -------------------------------------------------------------------------------- /src/app/not-found/not-found.component.html: -------------------------------------------------------------------------------- 1 |
2 |

404 Page Not Found

3 |
Oops! The content you are looking for does not exist in your Cosmic JS Bucket.
4 |
5 | -------------------------------------------------------------------------------- /src/app/page/page/page.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |

{{page.title}}

4 |
5 |
6 |
7 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "types": [] 6 | }, 7 | "exclude": [ 8 | "test.ts", 9 | "**/*.spec.ts" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: false, 3 | read_key: '##COSMIC_READ_KEY##', 4 | write_key: '##COSMIC_WRITE_KEY##', 5 | bucket_slug: '##COSMIC_BUCKET##', 6 | URL: 'https://api.cosmicjs.com/v1/' 7 | }; 8 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true, 3 | read_key: '##COSMIC_READ_KEY##', 4 | write_key: '##COSMIC_WRITE_KEY##', 5 | bucket_slug: '##COSMIC_BUCKET##', 6 | URL: 'https://api.cosmicjs.com/v1/' 7 | }; 8 | -------------------------------------------------------------------------------- /src/app/core/core.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | 4 | @NgModule({ 5 | declarations: [], 6 | imports: [ 7 | CommonModule 8 | ] 9 | }) 10 | export class CoreModule { } 11 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.scss'] 7 | }) 8 | export class AppComponent { 9 | title = 'angular-starter'; 10 | } 11 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | printWidth: 140, 3 | tabWidth: 2, 4 | useTabs: false, 5 | semi: true, 6 | singleQuote: true, 7 | trailingComma: "none", // other options `es5` or `all` 8 | bracketSpacing: true, 9 | arrowParens: "avoid", // other option 'always' 10 | parser: "typescript" 11 | }; 12 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "test.ts", 12 | "polyfills.ts" 13 | ], 14 | "include": [ 15 | "**/*.spec.ts", 16 | "**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AngularStarter 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/app/not-found/not-found.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-not-found', 5 | templateUrl: './not-found.component.html', 6 | styleUrls: ['./not-found.component.scss'] 7 | }) 8 | export class NotFoundComponent implements OnInit { 9 | 10 | constructor() { } 11 | 12 | ngOnInit() { 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('workspace-project App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getTitleText()).toEqual('Welcome to angular-starter!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "app", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "app", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/app/core/cosmic.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { CosmicService } from './cosmic.service'; 4 | 5 | describe('CosmicService', () => { 6 | beforeEach(() => TestBed.configureTestingModule({})); 7 | 8 | it('should be created', () => { 9 | const service: CosmicService = TestBed.get(CosmicService); 10 | expect(service).toBeTruthy(); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/app/page/page.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | 4 | import { PageRoutingModule } from './page-routing.module'; 5 | import { PageComponent } from './page/page.component'; 6 | 7 | @NgModule({ 8 | declarations: [PageComponent], 9 | imports: [ 10 | CommonModule, 11 | PageRoutingModule 12 | ] 13 | }) 14 | export class PageModule { } 15 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # 5 | # For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed 6 | 7 | > 0.5% 8 | last 2 versions 9 | Firefox ESR 10 | not dead 11 | not IE 9-11 -------------------------------------------------------------------------------- /scripts/objects.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "title": "Home", 4 | "slug": "home", 5 | "content": "This is the home page content.", 6 | "type_slug": "pages" 7 | }, 8 | { 9 | "title": "About", 10 | "slug": "about", 11 | "content": "This is the about page content.", 12 | "type_slug": "pages" 13 | }, 14 | { 15 | "title": "Contact", 16 | "slug": "contact", 17 | "content": "This is the contact page content.", 18 | "type_slug": "pages" 19 | } 20 | ] -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | html { 2 | font-family: sans-serif; 3 | } 4 | body { 5 | margin: 0; 6 | padding: 0; 7 | } 8 | .main, .footer { 9 | padding: 0 20px 20px; 10 | max-width: 960px; 11 | margin: 0 auto; 12 | } 13 | .hero { 14 | background: #29ABE2; 15 | width: 100%; 16 | } 17 | .hero h1 { 18 | margin: 0; 19 | font-size: 30px; 20 | padding: 30px; 21 | max-width: 960px; 22 | margin: 0 auto; 23 | } 24 | .hero h1 a { 25 | color: #fff; 26 | text-decoration: none; 27 | } 28 | -------------------------------------------------------------------------------- /src/app/page/page-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | import { PageComponent } from './page/page.component'; 4 | 5 | const routes: Routes = [ 6 | { path: '', component: PageComponent }, 7 | { 8 | path: ':slug', 9 | component: PageComponent 10 | } 11 | ]; 12 | 13 | @NgModule({ 14 | imports: [RouterModule.forChild(routes)], 15 | exports: [RouterModule] 16 | }) 17 | export class PageRoutingModule {} 18 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "importHelpers": true, 13 | "target": "es5", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | import { PageComponent } from './page/page/page.component'; 4 | import { NotFoundComponent } from './not-found/not-found.component'; 5 | 6 | const routes: Routes = [ 7 | { 8 | path: '', 9 | component: PageComponent 10 | }, 11 | { 12 | path: 'not-found', 13 | component: NotFoundComponent 14 | } 15 | ]; 16 | 17 | @NgModule({ 18 | imports: [RouterModule.forRoot(routes)], 19 | exports: [RouterModule] 20 | }) 21 | export class AppRoutingModule {} 22 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |

Cosmic JS Angular Starter

2 |
3 | 4 | 5 | 11 |
12 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Starter 2 | Angular starter app powered by [Cosmic JS](https://cosmicjs.com) 🚀 3 | 4 | ## Installation 5 | Install via the [Cosmic CLI](https://github.com/cosmicjs/cosmic-cli). 6 | ```bash 7 | npm i -g cosmic-cli 8 | 9 | # Login to your Cosmic JS account 10 | cosmic login 11 | 12 | # Installs example content to a new or existing Bucket and downloads the app locally 13 | cosmic init angular-starter 14 | cd angular-starter 15 | cosmic start 16 | ``` 17 | ## [Angular CMS](https://cosmicjs.com/knowledge-base/angularjs-cms) 18 | Cosmic JS offers a [Headless CMS](https://cosmicjs.com/headless-cms) for your Angular websites and apps. 19 | -------------------------------------------------------------------------------- /src/app/core/cosmic.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { HttpClient } from '@angular/common/http'; 3 | import { map } from 'rxjs/operators'; 4 | import { environment } from '../../environments/environment'; 5 | 6 | @Injectable({ 7 | providedIn: 'root' 8 | }) 9 | export class CosmicService { 10 | constructor(private http: HttpClient) {} 11 | 12 | getPage(slug: string) { 13 | const url = `${environment.URL + environment.bucket_slug}/object/${slug}?read_key=${environment.read_key}`; 14 | return this.http.get(url).pipe( 15 | map(_ => { 16 | return _['object']; 17 | }) 18 | ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const path = require('path'); 3 | const http = require('http'); 4 | const bodyParser = require('body-parser'); 5 | 6 | const app = express(); 7 | 8 | app.use(bodyParser.json()); 9 | app.use(bodyParser.urlencoded({ extended: false })); 10 | 11 | app.use(express.static(path.join(__dirname, 'dist'))); 12 | 13 | app.get('*', (req, res) => { 14 | res.sendFile(path.join(__dirname, 'dist/index.html')); 15 | }); 16 | 17 | const port = process.env.PORT || '3000'; 18 | app.set('port', port); 19 | 20 | const server = http.createServer(app); 21 | 22 | server.listen(port, () => console.log(`> Ready on http://localhost:${port}`)); 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | yarn-error.log 34 | testem.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | /src/environments/environment.prod.ts -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /scripts/import.js: -------------------------------------------------------------------------------- 1 | const Cosmic = require('cosmicjs') 2 | const api = Cosmic() 3 | const COSMIC_BUCKET = process.env.COSMIC_BUCKET || 'node-starter' 4 | const bucket = api.bucket({ 5 | slug: COSMIC_BUCKET, 6 | read_key: process.env.COSMIC_READ_KEY, 7 | write_key: process.env.COSMIC_WRITE_KEY 8 | }) 9 | const default_objects = require('./objects') 10 | const default_object_type = require('./object-type') 11 | const importObjects = async () => { 12 | try { 13 | const res = await bucket.getObjects() 14 | if(res.status === 'empty') { 15 | bucket.addObjectType(default_object_type) 16 | default_objects.forEach(object => { 17 | bucket.addObject(object) 18 | }) 19 | } 20 | } catch (e) { 21 | console.log(e) 22 | } 23 | } 24 | importObjects() 25 | -------------------------------------------------------------------------------- /src/app/page/page/page.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { PageComponent } from './page.component'; 4 | 5 | describe('PageComponent', () => { 6 | let component: PageComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ PageComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(PageComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/not-found/not-found.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { NotFoundComponent } from './not-found.component'; 4 | 5 | describe('NotFoundComponent', () => { 6 | let component: NotFoundComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ NotFoundComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(NotFoundComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/page/page/page.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { CosmicService } from 'src/app/core/cosmic.service'; 3 | import { ActivatedRoute } from '@angular/router'; 4 | import { map, switchMap } from 'rxjs/operators'; 5 | 6 | @Component({ 7 | selector: 'app-page', 8 | templateUrl: './page.component.html', 9 | styleUrls: ['./page.component.scss'] 10 | }) 11 | export class PageComponent implements OnInit { 12 | constructor(private route: ActivatedRoute, private cosmicService: CosmicService) {} 13 | 14 | public page: any; 15 | 16 | ngOnInit() { 17 | this.route.paramMap 18 | .pipe( 19 | map(paramMap => paramMap.get('slug')), 20 | switchMap(slug => (slug ? this.cosmicService.getPage(slug) : this.cosmicService.getPage('home'))) 21 | ) 22 | .subscribe(page => (this.page = page)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; 4 | import { AppRoutingModule } from './app-routing.module'; 5 | import { AppComponent } from './app.component'; 6 | import { PageModule } from './page/page.module'; 7 | import { CoreModule } from './core/core.module'; 8 | import { HttpErrorInterceptor } from './core/http-error.interceptor'; 9 | import { NotFoundComponent } from './not-found/not-found.component'; 10 | 11 | @NgModule({ 12 | declarations: [AppComponent, NotFoundComponent], 13 | imports: [BrowserModule, AppRoutingModule, PageModule, CoreModule, HttpClientModule], 14 | providers: [ 15 | { 16 | provide: HTTP_INTERCEPTORS, 17 | useClass: HttpErrorInterceptor, 18 | multi: true 19 | } 20 | ], 21 | bootstrap: [AppComponent] 22 | }) 23 | export class AppModule {} 24 | -------------------------------------------------------------------------------- /src/app/core/http-error.interceptor.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { HttpInterceptor, HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http'; 3 | import { Observable, throwError, EMPTY } from 'rxjs'; 4 | import { Router } from '@angular/router'; 5 | import { catchError } from 'rxjs/operators'; 6 | 7 | @Injectable({ providedIn: 'root' }) 8 | export class HttpErrorInterceptor implements HttpInterceptor { 9 | constructor(private router: Router) {} 10 | intercept(req: HttpRequest, next: HttpHandler): Observable> { 11 | return next.handle(req).pipe( 12 | catchError(response => { 13 | if (response instanceof HttpErrorResponse && response.status === 404) { 14 | this.router.navigateByUrl('/not-found', { skipLocationChange: true }); 15 | return EMPTY; 16 | } else { 17 | return throwError(response); 18 | } 19 | }) 20 | ); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false 30 | }); 31 | }; -------------------------------------------------------------------------------- /set-env.ts: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const argv = require('yargs').argv; 3 | 4 | const environment = argv.environment ? `.${argv.environment}` : ''; 5 | 6 | const targetPath = `./src/environments/environment${environment}.ts`; 7 | 8 | fs.readFile(targetPath, 'utf8', function(readError, data) { 9 | if (readError) { 10 | return console.log(readError); 11 | } 12 | let result = data; 13 | 14 | if (process.env.COSMIC_BUCKET) { 15 | console.log('Updating COSMIC_BUCKET'); 16 | 17 | result = result.replace(/(bucket_slug:\s*')(.*)(',)/g, `$1${process.env.COSMIC_BUCKET}$3`); 18 | } 19 | if (process.env.COSMIC_READ_KEY) { 20 | console.log('Updating COSMIC_READ_KEY'); 21 | 22 | result = result.replace(/(read_key:\s*')(.*)(',)/g, `$1${process.env.COSMIC_READ_KEY}$3`); 23 | } 24 | if (process.env.COSMIC_WRITE_KEY) { 25 | console.log('Updating COSMIC_WRITE_KEY'); 26 | 27 | result = result.replace(/(write_key:\s*')(.*)(',)/g, `$1${process.env.COSMIC_WRITE_KEY}$3`); 28 | } 29 | 30 | fs.writeFile(targetPath, result, 'utf8', function(writeError) { 31 | if (writeError) { 32 | return console.log(writeError); 33 | } 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | import { AppComponent } from './app.component'; 4 | 5 | describe('AppComponent', () => { 6 | beforeEach(async(() => { 7 | TestBed.configureTestingModule({ 8 | imports: [ 9 | RouterTestingModule 10 | ], 11 | declarations: [ 12 | AppComponent 13 | ], 14 | }).compileComponents(); 15 | })); 16 | 17 | it('should create the app', () => { 18 | const fixture = TestBed.createComponent(AppComponent); 19 | const app = fixture.debugElement.componentInstance; 20 | expect(app).toBeTruthy(); 21 | }); 22 | 23 | it(`should have as title 'angular-starter'`, () => { 24 | const fixture = TestBed.createComponent(AppComponent); 25 | const app = fixture.debugElement.componentInstance; 26 | expect(app.title).toEqual('angular-starter'); 27 | }); 28 | 29 | it('should render title in a h1 tag', () => { 30 | const fixture = TestBed.createComponent(AppComponent); 31 | fixture.detectChanges(); 32 | const compiled = fixture.debugElement.nativeElement; 33 | expect(compiled.querySelector('h1').textContent).toContain('Welcome to angular-starter!'); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-starter", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "config": "ts-node ./set-env.ts", 7 | "develop": "npm run config && ng build && node server.js", 8 | "start": "npm run config -- --environment=prod && ng build --prod --aot && NODE_ENV=production node server.js", 9 | "build": "ng build", 10 | "test": "ng test", 11 | "lint": "ng lint", 12 | "e2e": "ng e2e", 13 | "import": "node ./scripts/import.js" 14 | }, 15 | "private": true, 16 | "dependencies": { 17 | "@angular/animations": "~7.1.0", 18 | "@angular/common": "~7.1.0", 19 | "@angular/compiler": "~7.1.0", 20 | "@angular/core": "~7.1.0", 21 | "@angular/forms": "~7.1.0", 22 | "@angular/platform-browser": "~7.1.0", 23 | "@angular/platform-browser-dynamic": "~7.1.0", 24 | "@angular/router": "~7.1.0", 25 | "body-parser": "^1.18.3", 26 | "core-js": "^2.5.4", 27 | "cosmicjs": "^3.2.40", 28 | "express": "^4.16.4", 29 | "rxjs": "~6.3.3", 30 | "tslib": "^1.9.0", 31 | "zone.js": "~0.8.26" 32 | }, 33 | "devDependencies": { 34 | "@angular-devkit/build-angular": "~0.11.0", 35 | "@angular/cli": "~7.1.1", 36 | "@angular/compiler-cli": "~7.1.0", 37 | "@angular/language-service": "~7.1.0", 38 | "@types/node": "~8.9.4", 39 | "@types/jasmine": "~2.8.8", 40 | "@types/jasminewd2": "~2.0.3", 41 | "codelyzer": "~4.5.0", 42 | "jasmine-core": "~2.99.1", 43 | "jasmine-spec-reporter": "~4.2.1", 44 | "karma": "~3.1.1", 45 | "karma-chrome-launcher": "~2.2.0", 46 | "karma-coverage-istanbul-reporter": "~2.0.1", 47 | "karma-jasmine": "~1.1.2", 48 | "karma-jasmine-html-reporter": "^0.2.2", 49 | "protractor": "~5.4.0", 50 | "ts-node": "~7.0.0", 51 | "tslint": "~5.11.0", 52 | "typescript": "~3.1.6" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "codelyzer" 4 | ], 5 | "rules": { 6 | "arrow-return-shorthand": true, 7 | "callable-types": true, 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "deprecation": { 15 | "severity": "warn" 16 | }, 17 | "eofline": true, 18 | "forin": true, 19 | "import-blacklist": [ 20 | true, 21 | "rxjs/Rx" 22 | ], 23 | "import-spacing": true, 24 | "indent": [ 25 | true, 26 | "spaces" 27 | ], 28 | "interface-over-type-literal": true, 29 | "label-position": true, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-arg": true, 47 | "no-bitwise": true, 48 | "no-console": [ 49 | true, 50 | "debug", 51 | "info", 52 | "time", 53 | "timeEnd", 54 | "trace" 55 | ], 56 | "no-construct": true, 57 | "no-debugger": true, 58 | "no-duplicate-super": true, 59 | "no-empty": false, 60 | "no-empty-interface": true, 61 | "no-eval": true, 62 | "no-inferrable-types": [ 63 | true, 64 | "ignore-params" 65 | ], 66 | "no-misused-new": true, 67 | "no-non-null-assertion": true, 68 | "no-redundant-jsdoc": true, 69 | "no-shadowed-variable": true, 70 | "no-string-literal": false, 71 | "no-string-throw": true, 72 | "no-switch-case-fall-through": true, 73 | "no-trailing-whitespace": true, 74 | "no-unnecessary-initializer": true, 75 | "no-unused-expression": true, 76 | "no-use-before-declare": true, 77 | "no-var-keyword": true, 78 | "object-literal-sort-keys": false, 79 | "one-line": [ 80 | true, 81 | "check-open-brace", 82 | "check-catch", 83 | "check-else", 84 | "check-whitespace" 85 | ], 86 | "prefer-const": true, 87 | "quotemark": [ 88 | true, 89 | "single" 90 | ], 91 | "radix": true, 92 | "semicolon": [ 93 | true, 94 | "always" 95 | ], 96 | "triple-equals": [ 97 | true, 98 | "allow-null-check" 99 | ], 100 | "typedef-whitespace": [ 101 | true, 102 | { 103 | "call-signature": "nospace", 104 | "index-signature": "nospace", 105 | "parameter": "nospace", 106 | "property-declaration": "nospace", 107 | "variable-declaration": "nospace" 108 | } 109 | ], 110 | "unified-signatures": true, 111 | "variable-name": false, 112 | "whitespace": [ 113 | true, 114 | "check-branch", 115 | "check-decl", 116 | "check-operator", 117 | "check-separator", 118 | "check-type" 119 | ], 120 | "no-output-on-prefix": true, 121 | "use-input-property-decorator": true, 122 | "use-output-property-decorator": true, 123 | "use-host-property-decorator": true, 124 | "no-input-rename": true, 125 | "no-output-rename": true, 126 | "use-life-cycle-interface": true, 127 | "use-pipe-transform-interface": true, 128 | "component-class-suffix": true, 129 | "directive-class-suffix": true 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** 38 | * If the application will be indexed by Google Search, the following is required. 39 | * Googlebot uses a renderer based on Chrome 41. 40 | * https://developers.google.com/search/docs/guides/rendering 41 | **/ 42 | // import 'core-js/es6/array'; 43 | 44 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 45 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 46 | 47 | /** IE10 and IE11 requires the following for the Reflect API. */ 48 | // import 'core-js/es6/reflect'; 49 | 50 | /** 51 | * Web Animations `@angular/platform-browser/animations` 52 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 53 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 54 | **/ 55 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 56 | 57 | /** 58 | * By default, zone.js will patch all possible macroTask and DomEvents 59 | * user can disable parts of macroTask/DomEvents patch by setting following flags 60 | */ 61 | 62 | // (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 63 | // (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 64 | // (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 65 | 66 | /* 67 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 68 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 69 | */ 70 | // (window as any).__Zone_enable_cross_context_check = true; 71 | 72 | /*************************************************************************************************** 73 | * Zone JS is required by default for Angular itself. 74 | */ 75 | import 'zone.js/dist/zone'; // Included with Angular CLI. 76 | 77 | 78 | /*************************************************************************************************** 79 | * APPLICATION IMPORTS 80 | */ 81 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angular-starter": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "app", 11 | "schematics": { 12 | "@schematics/angular:component": { 13 | "styleext": "scss" 14 | } 15 | }, 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "src/tsconfig.app.json", 25 | "assets": ["src/favicon.ico", "src/assets"], 26 | "styles": ["src/styles.scss"], 27 | "scripts": [] 28 | }, 29 | "configurations": { 30 | "production": { 31 | "fileReplacements": [ 32 | { 33 | "replace": "src/environments/environment.ts", 34 | "with": "src/environments/environment.prod.ts" 35 | } 36 | ], 37 | "optimization": true, 38 | "outputHashing": "all", 39 | "sourceMap": false, 40 | "extractCss": true, 41 | "namedChunks": false, 42 | "aot": true, 43 | "extractLicenses": true, 44 | "vendorChunk": false, 45 | "buildOptimizer": true, 46 | "budgets": [ 47 | { 48 | "type": "initial", 49 | "maximumWarning": "2mb", 50 | "maximumError": "5mb" 51 | } 52 | ] 53 | } 54 | } 55 | }, 56 | "serve": { 57 | "builder": "@angular-devkit/build-angular:dev-server", 58 | "options": { 59 | "browserTarget": "angular-starter:build" 60 | }, 61 | "configurations": { 62 | "production": { 63 | "browserTarget": "angular-starter:build:production" 64 | } 65 | } 66 | }, 67 | "extract-i18n": { 68 | "builder": "@angular-devkit/build-angular:extract-i18n", 69 | "options": { 70 | "browserTarget": "angular-starter:build" 71 | } 72 | }, 73 | "test": { 74 | "builder": "@angular-devkit/build-angular:karma", 75 | "options": { 76 | "main": "src/test.ts", 77 | "polyfills": "src/polyfills.ts", 78 | "tsConfig": "src/tsconfig.spec.json", 79 | "karmaConfig": "src/karma.conf.js", 80 | "styles": ["src/styles.scss"], 81 | "scripts": [], 82 | "assets": ["src/favicon.ico", "src/assets"] 83 | } 84 | }, 85 | "lint": { 86 | "builder": "@angular-devkit/build-angular:tslint", 87 | "options": { 88 | "tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"], 89 | "exclude": ["**/node_modules/**"] 90 | } 91 | } 92 | } 93 | }, 94 | "angular-starter-e2e": { 95 | "root": "e2e/", 96 | "projectType": "application", 97 | "prefix": "", 98 | "architect": { 99 | "e2e": { 100 | "builder": "@angular-devkit/build-angular:protractor", 101 | "options": { 102 | "protractorConfig": "e2e/protractor.conf.js", 103 | "devServerTarget": "angular-starter:serve" 104 | }, 105 | "configurations": { 106 | "production": { 107 | "devServerTarget": "angular-starter:serve:production" 108 | } 109 | } 110 | }, 111 | "lint": { 112 | "builder": "@angular-devkit/build-angular:tslint", 113 | "options": { 114 | "tsConfig": "e2e/tsconfig.e2e.json", 115 | "exclude": ["**/node_modules/**"] 116 | } 117 | } 118 | } 119 | } 120 | }, 121 | "defaultProject": "angular-starter" 122 | } 123 | --------------------------------------------------------------------------------