Skip to content

Commit bc6b36b

Browse files
committed
fix builds and docs for alpha release
1 parent 88efea6 commit bc6b36b

File tree

10 files changed

+121
-89
lines changed

10 files changed

+121
-89
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ When stable, check examples folder for the documentation.
1515
npm install && parcel examples/blog/index.html
1616
```
1717

18+
19+
### Current Status
20+
21+
- @emberx/component - Test coverage: ✔️
22+
- @emberx/string - Test coverage: ✔️
23+
- @emberx/helper - Test coverage: ✔️
24+
- @emberx/test-helpers - Test coverage: ✔️
25+
- @emberx/router - Test coverage: ~70%. Missing: query param handling, loading states, error states, this.paramsFor()
26+
- @emberx/ssr - Test coverage: 0%. Waiting to finish @emberx.router.
27+
28+
Although the project is not production-ready one can still experiment with it using the blog example as a guide.
29+
`@emberx` packages are distributed on npm
30+
1831
### API Design
1932

2033
What if `@ember/routing/router` & `@ember/routing/route` were a tiny and explicitly resolvable router you could import from npm.

packages/@emberx/router/src/link-to.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface FreeObject {
66
[propName: string]: any;
77
}
88

9-
function isMissing<T>(value: Maybe<T>): value is null | undefined {
9+
function isMissing<T>(value: T): boolean {
1010
return value === null || value === undefined;
1111
}
1212

@@ -16,10 +16,11 @@ export default class extends Component<{
1616
tagName?: string;
1717
query?: FreeObject;
1818
preventDefault?: boolean;
19+
disabled?: boolean;
1920
replace?: boolean;
2021
route: string;
2122
}> {
22-
@service router;
23+
@service router: any;
2324

2425
get tagName() {
2526
return this.args.tagName || 'a';
@@ -29,7 +30,7 @@ export default class extends Component<{
2930
return 'preventDefault' in this.args ? this.args.preventDefault : false;
3031
}
3132

32-
constructor(owner, args) {
33+
constructor(owner: any, args: any) {
3334
super(owner, args);
3435

3536
if (!args.route) {
@@ -50,7 +51,8 @@ export default class extends Component<{
5051

5152
let linkWithParams = new URLSearchParams('');
5253

53-
Object.keys(this.args.query).forEach((key) => {
54+
Object.keys(this.args.query as object).forEach((key) => {
55+
// @ts-ignore
5456
linkWithParams.set(key, this.args.query[key]);
5557
});
5658

@@ -85,29 +87,32 @@ export default class extends Component<{
8587

8688
get models() {
8789
// NOTE: maybe optimize rendering if there is no dynamic segments?
88-
let dynamicSegments = this.router.recognizer.names[this.args.route].handlers.reduce(
89-
(result, handlerFunc) => {
90-
if (handlerFunc.shouldDecodes.length > 0) {
91-
result.push(handlerFunc.names);
92-
}
93-
94-
return result;
95-
},
96-
[]
97-
);
90+
// @ts-ignore
91+
let dynamicSegments = this.router.recognizer.names[this.args.route].handlers.reduce((result, handlerFunc) => {
92+
if (handlerFunc.shouldDecodes.length > 0) {
93+
result.push(handlerFunc.names);
94+
}
95+
96+
return result;
97+
}, []);
9898

9999
if (this.args.models) {
100+
// @ts-ignore
100101
return dynamicSegments.reduce((model, segment, index) => {
102+
// @ts-ignore
101103
return Object.assign(model, { [segment]: this.args.models[index] });
102104
}, {});
103105
} else if (isObject(this.args.model)) {
106+
// @ts-ignore
104107
return dynamicSegments.reduce((model, segment) => {
105108
let actualSegmentInModel = [segment[0], underscore(segment[0]), camelize(segment[0]), 'id'].find(
106109
(potentialKey) => {
110+
// @ts-ignore
107111
return potentialKey in this.args.model;
108112
}
109113
);
110114

115+
// @ts-ignore
111116
return Object.assign(model, { [segment]: this.args.model[actualSegmentInModel] });
112117
}, {});
113118
} else if (this.args.model) {
@@ -125,7 +130,8 @@ export default class extends Component<{
125130
</a>
126131
`;
127132

128-
@action transition(event) {
133+
@action transition(event: any) {
134+
// @ts-ignore
129135
let element = event.target;
130136
if (element.target === '' || element.target === '_self') {
131137
event.preventDefault();
@@ -159,6 +165,6 @@ export default class extends Component<{
159165
}
160166
}
161167

162-
function isObject(value) {
168+
function isObject(value: any) {
163169
return ['function', 'object'].includes(typeof value) && value !== null && !Array.isArray(value);
164170
}

packages/@emberx/router/src/route-map-context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export default class RouteMapContext {
1515
return Object.keys(routerJSRouteArray).map((registryRoute) => {
1616
let route = routerJSRouteArray[registryRoute];
1717
if (route.nestedRoutes.length > 0) {
18-
return match(route.options.path).to(route.routeName, function (match: any) {
18+
return match(route.options.path).to(route.name, function (match: any) {
1919
map(map, match, route.nestedRoutes);
2020
});
2121
}
2222

23-
match(route.options.path).to(route.routeName);
23+
match(route.options.path).to(route.name);
2424
});
2525
}
2626

@@ -39,14 +39,14 @@ export default class RouteMapContext {
3939
const existingIndexRoute = this.ROUTE_REGISTRY[`${this._parentRoute}.index`];
4040

4141
this.ROUTE_REGISTRY[`${this._parentRoute}.index`] = existingIndexRoute || {
42-
routeName: `${this._parentRoute}.index`,
42+
name: `${this._parentRoute}.index`,
4343
options: { path: '/' },
4444
route: undefined,
4545
};
4646
}
4747

4848
this.ROUTE_REGISTRY[this._parentRoute] = {
49-
routeName: this._parentRoute,
49+
name: this._parentRoute,
5050
options: targetOptions,
5151
route: undefined,
5252
};

packages/@emberx/router/src/route.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ export default class Route<Args extends FreeObject = {}> extends EmberXComponent
1818
</div>
1919
`;
2020

21-
static async setup(model: object, transition: FreeObject): any {
21+
static async setup(model: object, transition: FreeObject): Promise<any> {
2222
if (Router.LOG_MODELS) {
2323
console.log(`'${transition.targetName}' Route[model] is`, model);
2424
console.log(`'${transition.targetName}' Route[transition] is`, transition);
2525
}
2626

2727
// TODO: add a throw here if containerElement doesnt exist
2828

29-
const containerElement = globalThis.QUnit
29+
// @ts-ignore
30+
const containerElement: HTMLElement = globalThis.QUnit
3031
? document.getElementById('ember-testing')
3132
: document.getElementById('app');
3233

packages/@emberx/router/src/router-service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export default class RouterJSRouter extends Router<Route> {
4848
return;
4949
}
5050

51+
replaceURL(): void {
52+
return;
53+
}
54+
5155
transitionDidError(error: any, transition: any) {
5256
if (error.wasAborted || transition.isAborted) {
5357
// return logAbort(transition);
@@ -58,12 +62,12 @@ export default class RouterJSRouter extends Router<Route> {
5862
}
5963
}
6064

61-
routeWillChange(abc): void {
65+
routeWillChange(): void {
6266
// console.log('routeWillChange call for', abc);
6367
return;
6468
}
6569

66-
routeDidChange(abc): void {
70+
routeDidChange(): void {
6771
// console.log('routeDidChange call for', abc);
6872
return;
6973
}
@@ -72,15 +76,15 @@ export default class RouterJSRouter extends Router<Route> {
7276
return;
7377
}
7478

75-
willTransition(oldRouteInfos: any, newRouteInfos: any, transition) {
79+
willTransition(_oldRouteInfos: any, _newRouteInfos: any, transition: any) {
7680
let targetRouteInfo = transition.routeInfos[transition.resolveIndex];
7781

7882
this.currentRoute = targetRouteInfo._route;
7983
this.currentRouteName = targetRouteInfo.name;
8084
this.currentURL = targetRouteInfo.router.path;
8185
}
8286

83-
didTransition(routeInfos) {
87+
didTransition(routeInfos: any) {
8488
let targetRouteInfo = routeInfos[routeInfos.length - 1];
8589

8690
this.currentRoute = targetRouteInfo._route;
@@ -120,6 +124,7 @@ export default class RouterJSRouter extends Router<Route> {
120124

121125
// @ts-ignore
122126
console.log('targetParams', targetParams);
127+
// @ts-ignore
123128
await this.transitionTo(...targetParams);
124129
} else {
125130
console.log('targetHandler', targetHandler);

packages/@emberx/router/src/router.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface FreeObject {
1010
export interface RouteDefinition {
1111
path?: string;
1212
route: Route | undefined;
13-
routeName: string;
13+
name: string;
1414
options?: FreeObject;
1515
indexRoute?: Route;
1616
}
@@ -23,7 +23,7 @@ export interface routerJSRouteDefinition {
2323
path?: string;
2424
route: Route | undefined;
2525
options?: FreeObject;
26-
routeName: string;
26+
name: string;
2727
nestedRoutes?: [routerJSRouteDefinition?];
2828
}
2929

@@ -35,13 +35,14 @@ export default class Router {
3535
static LOG_MODELS = true;
3636
static SERVICES: FreeObject = {};
3737
static ROUTE_REGISTRY: RouteRegistry = {};
38-
static ROUTER_SERVICE: RouterService | null = null;
38+
static ROUTER_SERVICE: RouterService;
3939

4040
// static IS_TESTING() {
4141
// return !!globalThis.QUnit;
4242
// }
4343

4444
static visit() {
45+
// @ts-ignore
4546
return this.ROUTER_SERVICE.visit(...arguments);
4647
}
4748

@@ -67,7 +68,9 @@ export default class Router {
6768
});
6869
}
6970

70-
static definitionsToRegistry(arrayOfRouteDefinitions: Array<RouteDefinition> = []): RouteRegistry {
71+
static definitionsToRegistry(
72+
arrayOfRouteDefinitions: Array<RouteDefinition> = []
73+
): RouteRegistry {
7174
arrayOfRouteDefinitions.forEach((routeDefinition: RouteDefinition) => {
7275
if (!routeDefinition.path) {
7376
throw new Error('One of the RouteDefinition on Router.start(RouteDefinition[]) misses "path" key');
@@ -88,14 +91,14 @@ export default class Router {
8891
const targetIndex = index >= routePathSegments.length ? routePathSegments.length - 1 : index;
8992

9093
checkInRouteRegistryOrCreateRoute(this.ROUTE_REGISTRY, {
91-
routeName: targetSegmentName,
94+
name: targetSegmentName,
9295
options: { path: `/${routePathSegments[targetIndex]}` },
9396
route: index === routeNameSegments.length - 1 ? routeDefinition.route : undefined,
9497
} as routerJSRouteDefinition);
9598

9699
if (currentSegment && !this.ROUTE_REGISTRY[`${currentSegment}.index`]) {
97100
this.ROUTE_REGISTRY[`${currentSegment}.index`] = {
98-
routeName: `${currentSegment}.index`,
101+
name: `${currentSegment}.index`,
99102
options: { path: '/' },
100103
route: undefined, // TODO: add index route from parent by default
101104
};
@@ -106,17 +109,19 @@ export default class Router {
106109

107110
if (routeDefinition.indexRoute && routeName !== 'index') {
108111
this.ROUTE_REGISTRY[`${routeName}.index`] = {
109-
routeName: `${routeName}.index`,
112+
name: `${routeName}.index`,
110113
options: { path: '/' },
111114
route: routeDefinition.indexRoute,
112115
};
113116
}
114117
});
115118

119+
// @ts-ignore
116120
if (!arrayOfRouteDefinitions.find((routeDefinition) => routeDefinition.path.startsWith('/*'))) {
117121
this.ROUTE_REGISTRY['not-found'] = {
118-
routeName: 'not-found',
122+
name: 'not-found',
119123
options: { path: '/*slug' },
124+
// @ts-ignore
120125
route: class NotFoundRoute extends Route {},
121126
};
122127
}
@@ -166,10 +171,11 @@ export default class Router {
166171

167172
export function createRouteNameFromRouteClass(routeClass: Route | void): string | void {
168173
if (routeClass) {
174+
// @ts-ignore
169175
return routeClass.name
170176
.replace(/Route$/g, '')
171177
.split('')
172-
.reduce((result, character, index) => {
178+
.reduce((result: string[], character: string, index: number) => {
173179
if (index === 0) {
174180
return character.toLowerCase();
175181
} else if (character.toUpperCase() === character) {
@@ -188,8 +194,8 @@ export function createRouteNameFromPath(routePath: string): string {
188194
}
189195

190196
function checkInRouteRegistryOrCreateRoute(registry: RouteRegistry, targetRoute: routerJSRouteDefinition) {
191-
const routeName = targetRoute.routeName;
192-
const foundRoute = registry[targetRoute.routeName];
197+
const routeName = targetRoute.name;
198+
const foundRoute = registry[targetRoute.name];
193199

194200
if (!foundRoute) {
195201
registry[routeName] = targetRoute;
@@ -198,9 +204,9 @@ function checkInRouteRegistryOrCreateRoute(registry: RouteRegistry, targetRoute:
198204
}
199205

200206
if (targetRoute.route) {
201-
if (foundRoute.route && foundRoute.routeName !== targetRoute.routeName) {
207+
if (foundRoute.route && foundRoute.name !== targetRoute.name) {
202208
console.log(
203-
`[WARNING]: ${routeName}.route already has ${foundRoute.routeName}. You tried to overwrite ${routeName}.route with ${targetRoute.routeName}`
209+
`[WARNING]: ${routeName}.route already has ${foundRoute.name}. You tried to overwrite ${routeName}.route with ${targetRoute.name}`
204210
);
205211
}
206212

@@ -215,8 +221,8 @@ function findNestedRoute(routerJSRouteArray: Array<routerJSRouteDefinition>, rou
215221
const target = result.nestedRoutes || routerJSRouteArray;
216222
const targetRouteName = index === 0 ? routeNameSegment : routeNameSegments.slice(0, index + 1).join('.');
217223

218-
return target.find((routeObject: routerJSRouteDefinition) => routeObject.routeName === targetRouteName);
224+
return target.find((routeObject: routerJSRouteDefinition) => routeObject.name === targetRouteName);
219225
}, {} as FreeObject);
220226
}
221227

222-
window.Router = Router;
228+
// window.Router = Router;

0 commit comments

Comments
 (0)