Skip to content

Commit d2c1e05

Browse files
committed
fix(js:types): handle + add tests when routes are imported from JSON file
1 parent d35455c commit d2c1e05

File tree

7 files changed

+74
-25
lines changed

7 files changed

+74
-25
lines changed

Resources/js/router.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface QueryParamAddFunction {
1515
}
1616

1717
export interface Route {
18-
tokens: ([string, string] | [string, string, string] | [string, string, string, string] | [string, string, string, string, boolean])[];
18+
tokens: (string|boolean)[][];
1919
defaults: undefined[] | RouteDefaults;
2020
requirements: undefined[] | RouteRequirements;
2121
hosttokens: string[][];
@@ -33,7 +33,7 @@ export interface Context {
3333
host: string;
3434
port: string | null;
3535
scheme: string;
36-
locale: string;
36+
locale: string | null;
3737
}
3838

3939
export interface RoutingData {
@@ -43,7 +43,7 @@ export interface RoutingData {
4343
host: string;
4444
port?: string | null;
4545
scheme?: string;
46-
locale?: string;
46+
locale?: string | null;
4747
}
4848

4949
export class Router {
@@ -130,11 +130,11 @@ export class Router {
130130
return this.context_.port;
131131
};
132132

133-
setLocale(locale: string) {
133+
setLocale(locale: string | null) {
134134
this.context_.locale = locale;
135135
}
136136

137-
getLocale(): string {
137+
getLocale(): string | null {
138138
return this.context_.locale;
139139
};
140140

@@ -194,22 +194,22 @@ export class Router {
194194
let port = (typeof this.getPort() == 'undefined' || this.getPort() === null) ? '' : this.getPort();
195195

196196
route.tokens.forEach((token) => {
197-
if ('text' === token[0]) {
197+
if ('text' === token[0] && typeof token[1] === 'string') {
198198
url = Router.encodePathComponent(token[1]) + url;
199199
optional = false;
200200

201201
return;
202202
}
203203

204204
if ('variable' === token[0]) {
205-
let hasDefault = route.defaults && !Array.isArray(route.defaults) && token[3] && (token[3] in route.defaults);
206-
if (false === optional || !hasDefault || ((token[3] && token[3] in params) && !Array.isArray(route.defaults) && params[token[3]] != route.defaults[token[3]])) {
205+
let hasDefault = route.defaults && !Array.isArray(route.defaults) && typeof token[3] === 'string' && (token[3] in route.defaults);
206+
if (false === optional || !hasDefault || ((typeof token[3] === 'string' && token[3] in params) && !Array.isArray(route.defaults) && params[token[3]] != route.defaults[token[3]])) {
207207
let value;
208208

209-
if (token[3] && token[3] in params) {
209+
if (typeof token[3] === 'string' && token[3] in params) {
210210
value = params[token[3]];
211211
delete unusedParams[token[3]];
212-
} else if (token[3] && hasDefault && !Array.isArray(route.defaults)) {
212+
} else if (typeof token[3] === 'string' && hasDefault && !Array.isArray(route.defaults)) {
213213
value = route.defaults[token[3]];
214214
} else if (optional) {
215215
return;
@@ -230,7 +230,7 @@ export class Router {
230230
}
231231

232232
optional = false;
233-
} else if (hasDefault && (token[3] && token[3] in unusedParams)) {
233+
} else if (hasDefault && (typeof token[3] === 'string' && token[3] in unusedParams)) {
234234
delete unusedParams[token[3]];
235235
}
236236

Resources/public/js/router.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,22 @@ var Router = /*#__PURE__*/function () {
204204
var host = '';
205205
var port = typeof this.getPort() == 'undefined' || this.getPort() === null ? '' : this.getPort();
206206
route.tokens.forEach(function (token) {
207-
if ('text' === token[0]) {
207+
if ('text' === token[0] && typeof token[1] === 'string') {
208208
url = Router.encodePathComponent(token[1]) + url;
209209
optional = false;
210210
return;
211211
}
212212

213213
if ('variable' === token[0]) {
214-
var hasDefault = route.defaults && !Array.isArray(route.defaults) && token[3] && token[3] in route.defaults;
214+
var hasDefault = route.defaults && !Array.isArray(route.defaults) && typeof token[3] === 'string' && token[3] in route.defaults;
215215

216-
if (false === optional || !hasDefault || token[3] && token[3] in params && !Array.isArray(route.defaults) && params[token[3]] != route.defaults[token[3]]) {
216+
if (false === optional || !hasDefault || typeof token[3] === 'string' && token[3] in params && !Array.isArray(route.defaults) && params[token[3]] != route.defaults[token[3]]) {
217217
var value;
218218

219-
if (token[3] && token[3] in params) {
219+
if (typeof token[3] === 'string' && token[3] in params) {
220220
value = params[token[3]];
221221
delete unusedParams[token[3]];
222-
} else if (token[3] && hasDefault && !Array.isArray(route.defaults)) {
222+
} else if (typeof token[3] === 'string' && hasDefault && !Array.isArray(route.defaults)) {
223223
value = route.defaults[token[3]];
224224
} else if (optional) {
225225
return;
@@ -240,7 +240,7 @@ var Router = /*#__PURE__*/function () {
240240
}
241241

242242
optional = false;
243-
} else if (hasDefault && token[3] && token[3] in unusedParams) {
243+
} else if (hasDefault && typeof token[3] === 'string' && token[3] in unusedParams) {
244244
delete unusedParams[token[3]];
245245
}
246246

Resources/public/js/router.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/ts/router.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface QueryParamAddFunction {
1111
(prefix: string, params: any): void;
1212
}
1313
export interface Route {
14-
tokens: ([string, string] | [string, string, string] | [string, string, string, string] | [string, string, string, string, boolean])[];
14+
tokens: (string | boolean)[][];
1515
defaults: undefined[] | RouteDefaults;
1616
requirements: undefined[] | RouteRequirements;
1717
hosttokens: string[][];
@@ -27,7 +27,7 @@ export interface Context {
2727
host: string;
2828
port: string | null;
2929
scheme: string;
30-
locale: string;
30+
locale: string | null;
3131
}
3232
export interface RoutingData {
3333
base_url: string;
@@ -36,7 +36,7 @@ export interface RoutingData {
3636
host: string;
3737
port?: string | null;
3838
scheme?: string;
39-
locale?: string;
39+
locale?: string | null;
4040
}
4141
export declare class Router {
4242
private context_;
@@ -56,8 +56,8 @@ export declare class Router {
5656
getHost(): string;
5757
setPort(port: string | null): void;
5858
getPort(): string | null;
59-
setLocale(locale: string): void;
60-
getLocale(): string;
59+
setLocale(locale: string | null): void;
60+
getLocale(): string | null;
6161
/**
6262
* Builds query string params added to a URL.
6363
* Port of jQuery's $.param() function, so credit is due there.

Resources/ts/router.test-d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expectType } from 'tsd';
22
import { RoutesMap } from '../js/router';
33
import { Route, Router, Routing } from './router';
4+
import routes from './routes.json';
45

56
expectType<Router>(Router.getInstance());
67
expectType<Router>(Routing);
@@ -20,9 +21,10 @@ Routing.setHost('localhost');
2021
expectType<string | null>(Routing.getPort());
2122
Routing.setPort('1234');
2223

23-
expectType<string>(Routing.getLocale());
24+
expectType<string | null>(Routing.getLocale());
2425
Routing.setLocale('en');
2526

27+
Routing.setRoutingData(routes);
2628
Routing.setRoutingData({
2729
base_url: '',
2830
routes: {

Resources/ts/routes.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"base_url": "",
3+
"routes": {
4+
"bazinga_jstranslation_js": {
5+
"tokens": [
6+
[
7+
"variable",
8+
".",
9+
"js|json",
10+
"_format",
11+
true
12+
],
13+
[
14+
"variable",
15+
"\/",
16+
"[\\w]+",
17+
"domain",
18+
true
19+
],
20+
[
21+
"text",
22+
"\/translations"
23+
]
24+
],
25+
"defaults": {
26+
"domain": "messages",
27+
"_format": "js"
28+
},
29+
"requirements": {
30+
"_format": "js|json",
31+
"domain": "[\\w]+"
32+
},
33+
"hosttokens": [],
34+
"methods": [
35+
"GET"
36+
],
37+
"schemes": []
38+
}
39+
},
40+
"prefix": "",
41+
"host": "localhost",
42+
"port": "",
43+
"scheme": "https",
44+
"locale": null
45+
}

Resources/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"compilerOptions": {
33
"declaration": true,
4-
"declarationDir": "ts"
4+
"declarationDir": "ts",
5+
"resolveJsonModule": true,
6+
"esModuleInterop": true
57
}
68
}

0 commit comments

Comments
 (0)