Skip to content

Commit bc6184c

Browse files
authored
Merge pull request #99 from karthik2804/sdk/add_itty_router
add router to the SDK
2 parents 569b76d + a7173fe commit bc6184c

File tree

7 files changed

+162
-10
lines changed

7 files changed

+162
-10
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/** @internal */
2+
import { Router as _router } from 'itty-router'
3+
import { HttpRequest } from './spinSdk'
4+
5+
declare type GenericTraps = {
6+
[key: string]: any;
7+
};
8+
9+
export declare type RequestLike = {
10+
method: string;
11+
url: string;
12+
} & GenericTraps;
13+
14+
declare type IRequest = {
15+
method: string;
16+
url: string;
17+
params: {
18+
[key: string]: string;
19+
};
20+
query: {
21+
[key: string]: string | string[] | undefined;
22+
};
23+
proxy?: any;
24+
} & GenericTraps;
25+
26+
interface RouteHandler {
27+
(request: IRequest, ...args: any): any;
28+
}
29+
30+
declare type RouteEntry = [string, RegExp, RouteHandler[]];
31+
declare type Route = <T extends RouterType>(path: string, ...handlers: RouteHandler[]) => T;
32+
declare type RouterHints = {
33+
all: Route;
34+
delete: Route;
35+
get: Route;
36+
options: Route;
37+
patch: Route;
38+
post: Route;
39+
put: Route;
40+
};
41+
declare type RouterType = {
42+
__proto__: RouterType;
43+
routes: RouteEntry[];
44+
handle: (request: RequestLike, ...extra: any) => Promise<any>;
45+
} & RouterHints;
46+
47+
interface routerType {
48+
all(path: string, ...handlers: RouteHandler[]): RouterType
49+
delete(path: string, ...handlers: RouteHandler[]): RouterType
50+
get(path: string, ...handlers: RouteHandler[]): RouterType
51+
handle(request: RequestLike, ...extras: any): Promise<any>
52+
handleRequest(request: HttpRequest, ...extras: any): Promise<any>
53+
options(path: string, ...handlers: RouteHandler[]): RouterType
54+
patch(path: string, ...handlers: RouteHandler[]): RouterType
55+
post(path: string, ...handlers: RouteHandler[]): RouterType
56+
put(path: string, ...handlers: RouteHandler[]): RouterType
57+
routes: RouteEntry[]
58+
}
59+
60+
/** @internal */
61+
function router(): routerType {
62+
let _spinRouter = _router()
63+
64+
return {
65+
all: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.all(path, ...handlers) },
66+
delete: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.delete(path, ...handlers) },
67+
get: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.get(path, ...handlers) },
68+
handle:function (request: RequestLike, ...extra: any): Promise<any> { return _spinRouter.handle(request, ...extra) },
69+
handleRequest: function (request: HttpRequest, ...a: any): Promise<any> {
70+
return _spinRouter.handle({
71+
method: request.method,
72+
url: request.headers["spin-full-url"]
73+
}, ...a)
74+
},
75+
options: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.options(path, ...handlers) },
76+
patch: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.patch(path, ...handlers) },
77+
post: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.post(path, ...handlers) },
78+
put: function (path: string, ...handlers: RouteHandler[]): RouterType { return _spinRouter.put(path, ...handlers) },
79+
routes: _spinRouter.routes
80+
}
81+
}
82+
83+
/** @internal */
84+
export { router }
85+
export { routerType }

crates/spin-js-engine/src/js_sdk/modules/utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
/** @internal */
22
const arrayBufToBuf = require('typedarray-to-buffer')
3-
43
/** @internal */
5-
function toBuffer(arg0: ArrayBuffer): Buffer {
6-
return arrayBufToBuf(arg0)
7-
}
4+
import { router} from "./router"
5+
import { routerType} from "./router"
86

97
/** @internal */
108
const utils = {
119
toBuffer(arg0: ArrayBuffer): Buffer {
1210
return arrayBufToBuf(arg0)
13-
}
11+
},
12+
Router: () => {
13+
return router()
14+
}
1415
}
1516

1617
declare global {
1718
const utils: {
1819
toBuffer(argo: ArrayBuffer): Buffer
20+
Router(): routerType
1921
}
2022
}
2123

crates/spin-js-engine/src/js_sdk/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spin-js-engine/src/js_sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"buffer": "^6.0.3",
1313
"fast-text-encoding": "^1.0.6",
14+
"itty-router": "^3.0.11",
1415
"query-string": "^7.1.1",
1516
"typedarray-to-buffer": "^4.0.0",
1617
"url-parse": "^1.5.10"

crates/spin-js-engine/src/js_sdk/sdk.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import "./modules/glob"
1717
import { atob, btoa, Buffer } from "./modules/stringHandling"
1818

1919
/** @internal */
20-
import {crypto} from "./modules/crypto"
20+
import { crypto } from "./modules/crypto"
2121

2222
/** @internal */
2323
import "./modules/random"
@@ -27,11 +27,12 @@ import { URL, URLSearchParams } from "./modules/url"
2727
import "./modules/url"
2828

2929
/** @internal */
30-
import {utils} from "./modules/utils"
30+
import { utils } from "./modules/utils"
3131
import "./modules/utils"
3232

33+
3334
/** @internal */
34-
export { atob, btoa, Buffer, fetch, fsPromises, glob, crypto, URL, URLSearchParams, utils}
35+
export { atob, btoa, Buffer, fetch, fsPromises, glob, crypto, URL, URLSearchParams, utils }
3536

3637
// Stuff to be exported to the sdk types file
37-
export { HttpRequest, HttpResponse, HandleRequest}
38+
export { HttpRequest, HttpResponse, HandleRequest }

types/lib/modules/router.d.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { HttpRequest } from './spinSdk';
2+
declare type GenericTraps = {
3+
[key: string]: any;
4+
};
5+
export declare type RequestLike = {
6+
method: string;
7+
url: string;
8+
} & GenericTraps;
9+
declare type IRequest = {
10+
method: string;
11+
url: string;
12+
params: {
13+
[key: string]: string;
14+
};
15+
query: {
16+
[key: string]: string | string[] | undefined;
17+
};
18+
proxy?: any;
19+
} & GenericTraps;
20+
interface RouteHandler {
21+
(request: IRequest, ...args: any): any;
22+
}
23+
declare type RouteEntry = [string, RegExp, RouteHandler[]];
24+
declare type Route = <T extends RouterType>(path: string, ...handlers: RouteHandler[]) => T;
25+
declare type RouterHints = {
26+
all: Route;
27+
delete: Route;
28+
get: Route;
29+
options: Route;
30+
patch: Route;
31+
post: Route;
32+
put: Route;
33+
};
34+
declare type RouterType = {
35+
__proto__: RouterType;
36+
routes: RouteEntry[];
37+
handle: (request: RequestLike, ...extra: any) => Promise<any>;
38+
} & RouterHints;
39+
interface routerType {
40+
all(path: string, ...handlers: RouteHandler[]): RouterType;
41+
delete(path: string, ...handlers: RouteHandler[]): RouterType;
42+
get(path: string, ...handlers: RouteHandler[]): RouterType;
43+
handle(request: RequestLike, ...extras: any): Promise<any>;
44+
handleRequest(request: HttpRequest, ...extras: any): Promise<any>;
45+
options(path: string, ...handlers: RouteHandler[]): RouterType;
46+
patch(path: string, ...handlers: RouteHandler[]): RouterType;
47+
post(path: string, ...handlers: RouteHandler[]): RouterType;
48+
put(path: string, ...handlers: RouteHandler[]): RouterType;
49+
routes: RouteEntry[];
50+
}
51+
export { routerType };

types/lib/modules/utils.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// <reference types="node" />
2+
import { routerType } from "./router";
23
declare global {
34
const utils: {
45
toBuffer(argo: ArrayBuffer): Buffer;
6+
Router(): routerType;
57
};
68
}
7-
export {};

0 commit comments

Comments
 (0)