Skip to content

Commit 39d5eb4

Browse files
committed
scaffolding
1 parent d1b61e4 commit 39d5eb4

17 files changed

+209
-257
lines changed

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
# Change these settings to your own preference
10+
indent_style = space
11+
indent_size = 2
12+
13+
# We recommend you to keep these unchanged
14+
end_of_line = lf
15+
charset = utf-8
16+
trim_trailing_whitespace = true
17+
insert_final_newline = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ node_modules
2929

3030
www/
3131
config.json
32+
typings

index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from './lib/v2/parent.ts'
2+
export * from './lib/v2/posts.ts'
3+
import wpApiPosts from './lib/v2/posts.ts'
4+
import {Type} from 'angular2/src/facade/lang';
5+
6+
export const WP_API_PROVIDERS: Type[] = [
7+
wpApiPosts
8+
]

lib/v2/index.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
APP_INITIALIZER,
3+
Inject,
4+
Injectable,
5+
OpaqueToken,
6+
provide,
7+
Provider
8+
} from '@angular/core';
9+
import { HTTP_PROVIDERS } from '@angular/http';
10+
11+
import { stripTrailingSlash } from './utils';
12+
import { WpApiAppConfig } from './interfaces';
13+
import { WpApiApp, WpApiConfig } from './tokens';
14+
15+
@Injectable()
16+
export class WpApi {
17+
constructor(
18+
private config: WpApiAppConfig
19+
) {
20+
console.log('config', config)
21+
}
22+
}
23+
24+
export const defaultWpApi = (config: WpApiAppConfig): Provider => {
25+
// remove a trailing slash
26+
config.baseUrl = stripTrailingSlash(config.baseUrl);
27+
return provide(WpApiConfig, {
28+
useValue: config
29+
});
30+
};
31+
32+
export const WPAPI_PROVIDERS: any[] = [
33+
HTTP_PROVIDERS,
34+
{
35+
provide: WpApi,
36+
deps: [WpApiConfig]
37+
},
38+
];

lib/v2/interfaces.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface WpApiAppConfig {
2+
baseUrl: string;
3+
}

lib/v2/parent.ts

+16-31
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
1-
import {Inject} from 'angular2/core';
1+
import {Injectable, Inject, Optional} from 'angular2/core';
22
import {Http} from 'angular2/http';
3+
import {isPresent} from 'angular2/src/facade/lang';
4+
import {CONST_EXPR} from 'angular2/src/facade/lang';
5+
import {OpaqueToken} from 'angular2/core';
36

4-
export Parent class {
5-
constructor(@Inject(Http) http:Http) {
7+
export const WP_API_BASE_URL: OpaqueToken = CONST_EXPR(new OpaqueToken('wpApiBaseUrl'));
68

9+
@Injectable()
10+
export class Parent {
11+
private _baseUrl: string = '';
12+
constructor(private http: Http, @Optional() @Inject(WP_API_BASE_URL) _baseUrl?: string) {
713

8-
this.baseUrl = $injector.get('WpApi').getBaseUrl();
9-
this.defaultHttpProperties = $injector.get('WpApi').getDefaultHttpProperties();
10-
this.$http = $injector.get('$http');
11-
14+
if (isPresent(_baseUrl)) {
15+
this._baseUrl = _baseUrl;
16+
}
17+
console.log('_baseUrl', _baseUrl)
1218
}
1319

14-
getList(suffix, params = {}, data = {}, headers = {}) {
15-
return this.$http(angular.merge({}, this.defaultHttpProperties, {
16-
method: 'GET',
17-
url: this.baseUrl + suffix,
18-
params,
19-
data,
20-
headers
21-
}));
22-
}
20+
getList(suffix, params = {}, data = {}, headers = {}) { }
2321

24-
get(suffix, params = {}, data = {}, headers = {}) {
25-
return this.$http(angular.merge({}, this.defaultHttpProperties, {
26-
method: 'GET',
27-
url: this.baseUrl + suffix,
28-
params,
29-
data,
30-
headers
31-
}));
32-
}
22+
get(suffix, params = {}, data = {}, headers = {}) {}
3323

34-
requiredInput(functionName, inputs) {
35-
angular.forEach(inputs, (value, name) => {
36-
if (typeof value !== 'undefined') return;
37-
throw new Error(`Parameter ${name} from function ${functionName} is required`);
38-
});
39-
}
24+
requiredInput(functionName, inputs) {}
4025
}

lib/v2/posts.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import Parent from './parent.service.js';
2-
3-
export default class extends Parent {
1+
import {Injectable, Inject, Optional} from 'angular2/core';
2+
import {Parent} from './parent.ts';
43

4+
@Injectable()
5+
export default class wpApiPosts extends Parent {
6+
57
}

lib/v2/tokens.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {OpaqueToken} from '@angular/core';
2+
3+
export const WpApiApp = new OpaqueToken('WpApiApp')
4+
export const WpApiConfig = new OpaqueToken('WpApiConfig');

lib/v2/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function stripTrailingSlash(value: string): string {
2+
// Is the last char a /
3+
if (value.substring(value.length - 1, value.length) === '/') {
4+
return value.substring(0, value.length - 1);
5+
} else {
6+
return value;
7+
}
8+
}

package.json

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
"name": "wp-api-angularjs",
33
"version": "2.0.0-rc3",
44
"description": "WordPress WP-API v2 client for AngularJs",
5-
"main": "dist/wp-api-angularjs.js",
5+
"main": "index.js",
66
"scripts": {
7-
"devserver": "webpack-dev-server --port 8080 --json --progress",
8-
"devserverV2": "webpack-dev-server --port 8080 --json --progress --config webpack.config.v2.js",
7+
"startv1": "webpack-dev-server --port 8090 --json --progress",
8+
"start": "webpack-dev-server --port 8080 --json --progress --config webpack.config.v2.js",
99
"build": "npm run dumpdev && npm run dumpprod",
1010
"dumpdev": "webpack --progress --colors --config webpack.config.dist.js -d",
1111
"watch": "webpack --watch --progress --colors --config webpack.config.dist.js -d",
12-
"dumpprod": "webpack --progress --colors --config webpack.config.dist.min.js -p"
12+
"dumpprod": "webpack --progress --colors --config webpack.config.dist.min.js -p",
13+
"postinstall": "typings install",
14+
"typings": "typings"
1315
},
1416
"repository": {
1517
"type": "git",
@@ -30,12 +32,16 @@
3032
},
3133
"homepage": "https://github.com/shprink/wp-api-angularjs",
3234
"devDependencies": {
33-
"@reactivex/rxjs": "5.0.0-alpha.4",
35+
"@angular/common": "^2.0.0-rc.3",
36+
"@angular/compiler": "^2.0.0-rc.3",
37+
"@angular/core": "^2.0.0-rc.3",
38+
"@angular/http": "^2.0.0-rc.3",
39+
"@angular/platform-browser": "^2.0.0-rc.3",
40+
"@angular/platform-browser-dynamic": "^2.0.0-rc.3",
3441
"angular": "^1.4.5",
35-
"angular2": "2.0.0-alpha.45",
3642
"babel-core": "^5.6.15",
3743
"babel-loader": "^5.2.2",
38-
"es6-shim": "^0.33.10",
44+
"core-js": "^2.4.0",
3945
"exports-loader": "^0.6.2",
4046
"expose-loader": "^0.6.0",
4147
"gulp": "^3.9.0",
@@ -44,13 +50,14 @@
4450
"json-loader": "^0.5.2",
4551
"ng-annotate-loader": "~0.0.6",
4652
"path": "^0.4.9",
47-
"reflect-metadata": "0.1.2",
48-
"ts-loader": "^0.6.0",
53+
"reflect-metadata": "^0.1.3",
54+
"rxjs": "5.0.0-beta.6",
55+
"ts-loader": "^0.8.2",
4956
"typescript": "1.6.2",
5057
"util": "^0.10.3",
5158
"webpack": "~1.10.0",
5259
"webpack-dev-server": "~1.10.0",
53-
"zone.js": "0.5.8"
60+
"zone.js": "^0.6.12"
5461
},
5562
"dependencies": {
5663
"js-base64": "^2.1.9"

test/v2/app.ts

+4-118
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,11 @@
1-
/*
2-
* Angular 2 decorators and services
3-
*/
4-
import {Directive, Component, View, ElementRef} from 'angular2/angular2';
5-
import {RouteConfig, Router} from 'angular2/router';
6-
import {Http, Headers} from 'angular2/http';
1+
import { Component } from '@angular/core';
72

8-
/*
9-
* Angular Directives
10-
*/
11-
import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2';
12-
import {ROUTER_DIRECTIVES} from 'angular2/router';
13-
14-
15-
/*
16-
* App Component
17-
* Top Level Component
18-
*/
193
@Component({
20-
// The selector is what angular internally uses
21-
// for `document.querySelectorAll(selector)` in our index.html
22-
// where, in this case, selector is the string 'app'
23-
selector: 'app', // <app></app>
24-
// We need to tell Angular's compiler which directives are in our template.
25-
// Doing so will allow Angular to attach our behavior to an element
26-
directives: [ CORE_DIRECTIVES, FORM_DIRECTIVES, ROUTER_DIRECTIVES ],
27-
// Our list of styles in our component. We may add more to compose many styles together
28-
styles: [`
29-
.title {
30-
font-family: Arial, Helvetica, sans-serif;
31-
}
32-
main {
33-
padding: 1em;
34-
}
35-
`],
36-
// Every Angular template is first compiled by the browser before Angular runs it's compiler
37-
template: `
38-
<header>
39-
<h1 class="title">Hello {{ title }}</h1>
40-
</header>
41-
42-
<main>
43-
Your Content Here
44-
<div>
45-
46-
<input type="text" [value]="title" (input)="title = $event.target.value" autofocus>
47-
<!--
48-
Rather than wiring up two-way data-binding ourselves
49-
we can use Angular's [(ng-model)] syntax
50-
<input type="text" [(ng-model)]="title">
51-
-->
52-
</div>
53-
54-
<pre>this.title = {{ title | json }}</pre>
55-
<pre>this.data = {{ data | json }}</pre>
56-
57-
</main>
58-
`
4+
selector: 'app',
5+
template: '<h1>My First Angular 2 App</h1>'
596
})
607
export class App {
61-
// These are member type
62-
title: string;
63-
data: Array<any> = []; // default data
64-
// TypeScript public modifiers
65-
constructor(public http: Http) {
66-
this.title = 'Angular 2';
8+
constructor() {
679
}
68-
69-
onInit() {
70-
// Our API
71-
// Before you start the app, run these commands in another process:
72-
//
73-
// - npm run express-install
74-
// - npm run express
75-
//
76-
// This will start a process that will listen for requests on port 3001
77-
78-
const BASE_URL = 'http://localhost:3001';
79-
const TODO_API_URL = '/api/todos';
80-
const JSON_HEADERS = new Headers();
81-
82-
JSON_HEADERS.append('Accept', 'application/json');
83-
JSON_HEADERS.append('Content-Type', 'application/json');
84-
85-
this.http
86-
.get(BASE_URL + TODO_API_URL, {
87-
headers: JSON_HEADERS
88-
})
89-
.map(res => res.json())
90-
.subscribe(
91-
// onNext callback
92-
data => this.serverData(data),
93-
// onError callback
94-
err => this.errorMessage(err),
95-
// onComplete callback
96-
() => console.log('complete')
97-
);//end http
98-
99-
}
100-
101-
serverData(data) {
102-
console.log('data', data);
103-
this.data = data;
104-
}//serverData
105-
106-
errorMessage(err) {
107-
console.info(`${'\n'
108-
} // You must run these commands in another process for the Http API to work ${'\n'
109-
} npm run express-install ${'\n'
110-
} npm run express
111-
`);
112-
}//errorMessage
113-
11410
}
11511

116-
117-
118-
/*
119-
* Please review the examples/ folder for more angular app examples
120-
* (The examples may not be updated as quickly. Please open an issue on github for us to update it)
121-
* you can change the `entry` in webpack.config to quickly view the examples
122-
* For help or questions please contact us at @AngularClass on twitter
123-
* or our chat on Slack at https://AngularClass.com/slack-join
124-
* or via chat on Gitter at https://gitter.im/AngularClass/angular2-webpack-starter
125-
*/

test/v2/bootstrap.ts

-26
This file was deleted.

test/v2/index.html

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
<app>
2020
Loading...
2121
</app>
22-
{% for (var chunk in o.htmlWebpackPlugin.files.chunks) { %}
23-
<script src="{%=o.htmlWebpackPlugin.files.chunks[chunk].entry %}"></script>
24-
{% } %}
2522
</body>
2623

2724
</html>

0 commit comments

Comments
 (0)