@owlmeans/server-entrypoint 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/build/entrypoint.d.ts +6 -0
- package/build/entrypoint.d.ts.map +1 -0
- package/build/entrypoint.js +38 -0
- package/build/entrypoint.js.map +1 -0
- package/build/helper.d.ts +5 -0
- package/build/helper.d.ts.map +1 -0
- package/build/helper.js +25 -0
- package/build/helper.js.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/build/types.d.ts +27 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/utils/entrypoint.d.ts +3 -0
- package/build/utils/entrypoint.d.ts.map +1 -0
- package/build/utils/entrypoint.js +3 -0
- package/build/utils/entrypoint.js.map +1 -0
- package/build/utils/helper.d.ts +2 -0
- package/build/utils/helper.d.ts.map +1 -0
- package/build/utils/helper.js +2 -0
- package/build/utils/helper.js.map +1 -0
- package/package.json +38 -0
- package/src/entrypoint.ts +51 -0
- package/src/helper.ts +38 -0
- package/src/index.ts +4 -0
- package/src/types.ts +31 -0
- package/src/utils/entrypoint.ts +3 -0
- package/src/utils/helper.ts +2 -0
- package/tsconfig.json +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @owlmeans/server-entrypoint
|
|
2
|
+
|
|
3
|
+
Elevates route definitions into runnable server entrypoints with attached request handlers.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
- `elevate(entrypoints, alias, handler?, opts?)` attaches a `RefedEntrypointHandler` to an existing entrypoint in place
|
|
8
|
+
- `entrypoint(commonEntrypoint, handler?, opts?)` wraps a `CommonEntrypoint` into a `ServerEntrypoint`
|
|
9
|
+
- `guard(alias, opts?)` creates `EntrypointOptions` requiring a named guard service
|
|
10
|
+
- Used internally by `@owlmeans/server-app`'s `elevate` re-export
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun add @owlmeans/server-entrypoint
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Typical pattern — define entrypoints separately, elevate with handlers:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { elevate, guard } from '@owlmeans/server-app'
|
|
24
|
+
import { handleBody, handleParams } from '@owlmeans/server-app'
|
|
25
|
+
|
|
26
|
+
const appModules = [
|
|
27
|
+
entrypoint(route('project-create', '/projects', backend(RouteMethod.POST)), guard('auth')),
|
|
28
|
+
entrypoint(route('project-get', '/projects/:id', backend())),
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
elevate(appModules, 'project-create', handleBody(async (payload, ctx) => {
|
|
32
|
+
return await ctx.project().create(payload)
|
|
33
|
+
}))
|
|
34
|
+
|
|
35
|
+
elevate(appModules, 'project-get', handleParams(async (params, ctx) => {
|
|
36
|
+
return await ctx.project().get(params.id)
|
|
37
|
+
}))
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
### `elevate<R>(entrypoints, alias, handler?, opts?): ServerEntrypoint<R>[]`
|
|
43
|
+
|
|
44
|
+
Mutates `entrypoints` in-place: finds the entrypoint with `alias` and attaches `handler`. Throws if the alias is not found or already elevated (unless `opts.force` is true).
|
|
45
|
+
|
|
46
|
+
### `entrypoint<R>(commonEntrypoint, handler?, opts?): ServerEntrypoint<R>`
|
|
47
|
+
|
|
48
|
+
Wraps a single `CommonEntrypoint` into a `ServerEntrypoint`. Lower-level than `elevate`.
|
|
49
|
+
|
|
50
|
+
### `guard<R>(guard, opts?): EntrypointOptions<R>`
|
|
51
|
+
|
|
52
|
+
Returns `EntrypointOptions` that require the named guard service to pass before the handler runs.
|
|
53
|
+
|
|
54
|
+
### `ServerEntrypoint<R>`
|
|
55
|
+
|
|
56
|
+
Extends `CommonEntrypoint` with:
|
|
57
|
+
- `route: ServerRouteModel<R>` — resolved server route
|
|
58
|
+
- `handle: RefedEntrypointHandler<R>` — the attached handler
|
|
59
|
+
|
|
60
|
+
### `RefedEntrypointHandler<R>`
|
|
61
|
+
|
|
62
|
+
A handler factory: `(ref: { ref?: { ctx?: Context } }) => EntrypointHandler`.
|
|
63
|
+
|
|
64
|
+
## Related Packages
|
|
65
|
+
|
|
66
|
+
- [`@owlmeans/entrypoint`](../entrypoint) — `CommonEntrypoint` base that gets elevated
|
|
67
|
+
- [`@owlmeans/server-api`](../server-api) — handler wrappers (`handleBody`, etc.) used with `elevate`
|
|
68
|
+
- [`@owlmeans/server-app`](../server-app) — re-exports `elevate`, `entrypoint`, `guard`
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ServerRouteModel } from '@owlmeans/server-route';
|
|
2
|
+
import type { ServerEntrypoint, EntrypointOptions, RefedEntrypointHandler } from './types.js';
|
|
3
|
+
import type { CommonEntrypoint } from '@owlmeans/entrypoint';
|
|
4
|
+
import type { CommonRouteModel } from '@owlmeans/route';
|
|
5
|
+
export declare const entrypoint: <R>(arg: CommonEntrypoint | ServerRouteModel<R> | CommonRouteModel, handler?: RefedEntrypointHandler<R>, opts?: EntrypointOptions<R>) => ServerEntrypoint<R>;
|
|
6
|
+
//# sourceMappingURL=entrypoint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrypoint.d.ts","sourceRoot":"","sources":["../src/entrypoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAC9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAiB,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAG5G,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAIvD,eAAO,MAAM,UAAU,GAAI,CAAC,EAC1B,KAAK,gBAAgB,GAAG,gBAAgB,CAAC,CAAC,CAAC,GAAG,gBAAgB,EAAE,UAAU,sBAAsB,CAAC,CAAC,CAAC,EAAE,OAAO,iBAAiB,CAAC,CAAC,CAAC,KAC/H,gBAAgB,CAAC,CAAC,CAuCpB,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isEntrypoint, makeCommonEntrypoint } from './utils/entrypoint.js';
|
|
2
|
+
import { isServerRouteModel, route } from '@owlmeans/server-route';
|
|
3
|
+
import { prependBase } from '@owlmeans/route/utils';
|
|
4
|
+
export const entrypoint = (arg, handler, opts) => {
|
|
5
|
+
const entrypointHandle = { ref: undefined };
|
|
6
|
+
let _entrypoint;
|
|
7
|
+
if (isEntrypoint(arg)) {
|
|
8
|
+
const routeModel = route(arg.route, opts?.intermediate ?? false, opts?.routeOptions);
|
|
9
|
+
_entrypoint = arg;
|
|
10
|
+
_entrypoint.route = routeModel;
|
|
11
|
+
_entrypoint.filter = opts?.filter ?? arg.filter;
|
|
12
|
+
_entrypoint.guards = [...(arg.guards ?? []), ...(opts?.guards ?? [])];
|
|
13
|
+
_entrypoint.gate = opts?.gate ?? arg.gate;
|
|
14
|
+
_entrypoint.gateParams = opts?.gateParams ?? arg.gateParams;
|
|
15
|
+
}
|
|
16
|
+
else if (isServerRouteModel(arg)) {
|
|
17
|
+
_entrypoint = makeCommonEntrypoint(arg, { ...opts });
|
|
18
|
+
_entrypoint.route = arg;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const _route = route(arg, opts?.intermediate ?? false, opts?.routeOptions);
|
|
22
|
+
_entrypoint = makeCommonEntrypoint(_route, { ...opts });
|
|
23
|
+
_entrypoint.route = _route;
|
|
24
|
+
}
|
|
25
|
+
_entrypoint.fixer = opts?.fixer;
|
|
26
|
+
if (handler != null) {
|
|
27
|
+
_entrypoint.handle = handler(entrypointHandle);
|
|
28
|
+
}
|
|
29
|
+
_entrypoint.reinitializeContext = (context) => {
|
|
30
|
+
const newEntrypoint = entrypoint(arg, handler, opts);
|
|
31
|
+
newEntrypoint.ctx = context;
|
|
32
|
+
return newEntrypoint;
|
|
33
|
+
};
|
|
34
|
+
_entrypoint.getPath = () => prependBase(_entrypoint.route.route);
|
|
35
|
+
entrypointHandle.ref = _entrypoint;
|
|
36
|
+
return _entrypoint;
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=entrypoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrypoint.js","sourceRoot":"","sources":["../src/entrypoint.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAA;AAC1E,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAIlE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAEnD,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,GAA8D,EAAE,OAAmC,EAAE,IAA2B,EAC3G,EAAE;IACvB,MAAM,gBAAgB,GAAqB,EAAE,GAAG,EAAE,SAAS,EAAE,CAAA;IAE7D,IAAI,WAAgC,CAAA;IAEpC,IAAI,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,YAAY,IAAI,KAAK,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;QACpF,WAAW,GAAG,GAA0B,CAAA;QACxC,WAAW,CAAC,KAAK,GAAG,UAAU,CAAA;QAC9B,WAAW,CAAC,MAAM,GAAG,IAAI,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,CAAA;QAC/C,WAAW,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,IAAI,EAAE,MAAM,I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"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ServerEntrypoint, EntrypointOptions, RefedEntrypointHandler } from './types.js';
|
|
2
|
+
import type { CommonEntrypoint } from '@owlmeans/entrypoint';
|
|
3
|
+
export declare const elevate: <R>(entrypoints: (CommonEntrypoint | ServerEntrypoint<R>)[], alias: string, handler?: RefedEntrypointHandler<R> | boolean | EntrypointOptions<R>, opts?: boolean | EntrypointOptions<R>) => ServerEntrypoint<R>[];
|
|
4
|
+
export declare const guard: <R>(guard: string, opts?: EntrypointOptions<R>) => EntrypointOptions<R>;
|
|
5
|
+
//# sourceMappingURL=helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAI7F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,eAAO,MAAM,OAAO,GAAI,CAAC,EACvB,aAAa,CAAC,gBAAgB,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC,EAAE,EACvD,OAAO,MAAM,EACb,UAAU,sBAAsB,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,iBAAiB,CAAC,CAAC,CAAC,EACpE,OAAO,OAAO,GAAG,iBAAiB,CAAC,CAAC,CAAC,KACpC,gBAAgB,CAAC,CAAC,CAAC,EAuBrB,CAAA;AAED,eAAO,MAAM,KAAK,GAAI,CAAC,EAAE,OAAO,MAAM,EAAE,OAAO,iBAAiB,CAAC,CAAC,CAAC,KAAG,iBAAiB,CAAC,CAAC,CACjD,CAAA"}
|
package/build/helper.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { entrypoint } from './entrypoint.js';
|
|
2
|
+
import { isServerRouteModel } from '@owlmeans/server-route';
|
|
3
|
+
import { createBasicGuard } from './utils/helper.js';
|
|
4
|
+
export const elevate = (entrypoints, alias, handler, opts) => {
|
|
5
|
+
const idx = entrypoints.findIndex(({ route }) => route.route.alias === alias);
|
|
6
|
+
if (idx === -1) {
|
|
7
|
+
throw new SyntaxError(`Entrypoint with alias ${alias} not present`);
|
|
8
|
+
}
|
|
9
|
+
if (typeof handler === 'boolean') {
|
|
10
|
+
opts = handler;
|
|
11
|
+
handler = undefined;
|
|
12
|
+
}
|
|
13
|
+
if (typeof handler === 'object' && typeof handler !== 'function') {
|
|
14
|
+
opts = handler;
|
|
15
|
+
handler = undefined;
|
|
16
|
+
}
|
|
17
|
+
const force = typeof opts === 'object' && opts.force;
|
|
18
|
+
if (isServerRouteModel(entrypoints[idx].route) && !force) {
|
|
19
|
+
throw new SyntaxError(`Entrypoint with alias ${alias} is already elevated`);
|
|
20
|
+
}
|
|
21
|
+
entrypoints[idx] = entrypoint(entrypoints[idx], handler, typeof opts === 'boolean' ? { intermediate: opts } : opts);
|
|
22
|
+
return entrypoints;
|
|
23
|
+
};
|
|
24
|
+
export const guard = (guard, opts) => ({ ...createBasicGuard(guard, opts) });
|
|
25
|
+
//# sourceMappingURL=helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAGpD,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,WAAuD,EACvD,KAAa,EACb,OAAoE,EACpE,IAAqC,EACd,EAAE;IACzB,MAAM,GAAG,GAAG,WAAW,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,CAAA;IAC7E,IAAI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/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAE3B,cAAc,iBAAiB,CAAA"}
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ServerRouteModel, ServerRouteOptions } from '@owlmeans/server-route';
|
|
2
|
+
import type { CommonEntrypoint, EntrypointHandler, CommonEntrypointOptions } from '@owlmeans/entrypoint';
|
|
3
|
+
import type { Service } from '@owlmeans/context';
|
|
4
|
+
export interface ServerEntrypoint<R> extends CommonEntrypoint {
|
|
5
|
+
route: ServerRouteModel<R>;
|
|
6
|
+
fixer?: string;
|
|
7
|
+
handle: EntrypointHandler;
|
|
8
|
+
}
|
|
9
|
+
export interface EntrypointOptions<R> extends CommonEntrypointOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Force entrypoint to be elevated even if it is already elevated
|
|
12
|
+
*/
|
|
13
|
+
force?: boolean;
|
|
14
|
+
fixer?: string;
|
|
15
|
+
intermediate?: boolean;
|
|
16
|
+
routeOptions?: ServerRouteOptions<R>;
|
|
17
|
+
}
|
|
18
|
+
export interface FixerService extends Service {
|
|
19
|
+
handle: <R>(reply: R, error: Error) => void;
|
|
20
|
+
}
|
|
21
|
+
export interface EntrypointRef<R> {
|
|
22
|
+
ref?: ServerEntrypoint<R>;
|
|
23
|
+
}
|
|
24
|
+
export interface RefedEntrypointHandler<R = {}> {
|
|
25
|
+
(ref: EntrypointRef<R>): EntrypointHandler;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAClF,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AACxG,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAEhD,MAAM,WAAW,gBAAgB,CAAC,CAAC,CAAE,SAAQ,gBAAgB;IAC3D,KAAK,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAA;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,iBAAiB,CAAA;CAC1B;AAED,MAAM,WAAW,iBAAiB,CAAC,CAAC,CAAE,SAAQ,uBAAuB;IACnE;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,YAAY,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAA;CACrC;AAED,MAAM,WAAW,YAAa,SAAQ,OAAO;IAC3C,MAAM,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;CAC5C;AAED,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,GAAG,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAA;CAC1B;AAED,MAAM,WAAW,sBAAsB,CAAC,CAAC,GAAG,EAAE;IAC5C,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAA;CAC3C"}
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrypoint.d.ts","sourceRoot":"","sources":["../../src/utils/entrypoint.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrypoint.js","sourceRoot":"","sources":["../../src/utils/entrypoint.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/utils/helper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,gBAAgB,EAAE,MAAM,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/utils/helper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,gBAAgB,EAAE,MAAM,sBAAsB,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@owlmeans/server-entrypoint",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc -b",
|
|
8
|
+
"dev": "sleep 300 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
|
|
9
|
+
"watch": "tsc -b -w --preserveWatchOutput --pretty"
|
|
10
|
+
},
|
|
11
|
+
"main": "build/index.js",
|
|
12
|
+
"module": "build/index.js",
|
|
13
|
+
"types": "build/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./build/index.js",
|
|
17
|
+
"require": "./build/index.js",
|
|
18
|
+
"default": "./build/index.js",
|
|
19
|
+
"module": "./build/index.js",
|
|
20
|
+
"types": "./build/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@owlmeans/context": "^0.1.3",
|
|
25
|
+
"@owlmeans/entrypoint": "^0.1.3",
|
|
26
|
+
"@owlmeans/route": "^0.1.3",
|
|
27
|
+
"@owlmeans/server-route": "^0.1.3"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@owlmeans/dep-config": "workspace:*",
|
|
31
|
+
"@types/node": "^24.10.1",
|
|
32
|
+
"nodemon": "^3.1.11",
|
|
33
|
+
"typescript": "^6.0.2"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ServerRouteModel } from '@owlmeans/server-route'
|
|
2
|
+
import type { ServerEntrypoint, EntrypointOptions, EntrypointRef, RefedEntrypointHandler } from './types.js'
|
|
3
|
+
import { isEntrypoint, makeCommonEntrypoint } from './utils/entrypoint.js'
|
|
4
|
+
import { isServerRouteModel, route } from '@owlmeans/server-route'
|
|
5
|
+
import type { CommonEntrypoint } from '@owlmeans/entrypoint'
|
|
6
|
+
import type { CommonRouteModel } from '@owlmeans/route'
|
|
7
|
+
import type { BasicContext } from '@owlmeans/context'
|
|
8
|
+
import { prependBase } from '@owlmeans/route/utils'
|
|
9
|
+
|
|
10
|
+
export const entrypoint = <R>(
|
|
11
|
+
arg: CommonEntrypoint | ServerRouteModel<R> | CommonRouteModel, handler?: RefedEntrypointHandler<R>, opts?: EntrypointOptions<R>
|
|
12
|
+
): ServerEntrypoint<R> => {
|
|
13
|
+
const entrypointHandle: EntrypointRef<R> = { ref: undefined }
|
|
14
|
+
|
|
15
|
+
let _entrypoint: ServerEntrypoint<R>
|
|
16
|
+
|
|
17
|
+
if (isEntrypoint(arg)) {
|
|
18
|
+
const routeModel = route(arg.route, opts?.intermediate ?? false, opts?.routeOptions)
|
|
19
|
+
_entrypoint = arg as ServerEntrypoint<R>
|
|
20
|
+
_entrypoint.route = routeModel
|
|
21
|
+
_entrypoint.filter = opts?.filter ?? arg.filter
|
|
22
|
+
_entrypoint.guards = [...(arg.guards ?? []), ...(opts?.guards ?? [])]
|
|
23
|
+
_entrypoint.gate = opts?.gate ?? arg.gate
|
|
24
|
+
_entrypoint.gateParams = opts?.gateParams ?? arg.gateParams
|
|
25
|
+
} else if (isServerRouteModel(arg)) {
|
|
26
|
+
_entrypoint = makeCommonEntrypoint(arg, { ...opts }) as ServerEntrypoint<R>
|
|
27
|
+
_entrypoint.route = arg
|
|
28
|
+
} else {
|
|
29
|
+
const _route = route(arg, opts?.intermediate ?? false, opts?.routeOptions)
|
|
30
|
+
_entrypoint = makeCommonEntrypoint(_route, { ...opts }) as ServerEntrypoint<R>
|
|
31
|
+
_entrypoint.route = _route
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_entrypoint.fixer = opts?.fixer
|
|
35
|
+
if (handler != null) {
|
|
36
|
+
_entrypoint.handle = handler(entrypointHandle)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_entrypoint.reinitializeContext = <T>(context: BasicContext<any>) => {
|
|
40
|
+
const newEntrypoint = entrypoint(arg, handler, opts)
|
|
41
|
+
newEntrypoint.ctx = context
|
|
42
|
+
|
|
43
|
+
return newEntrypoint as T
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
_entrypoint.getPath = () => prependBase(_entrypoint.route.route)
|
|
47
|
+
|
|
48
|
+
entrypointHandle.ref = _entrypoint
|
|
49
|
+
|
|
50
|
+
return _entrypoint
|
|
51
|
+
}
|
package/src/helper.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ServerEntrypoint, EntrypointOptions, RefedEntrypointHandler } from './types.js'
|
|
2
|
+
import { entrypoint } from './entrypoint.js'
|
|
3
|
+
import { isServerRouteModel } from '@owlmeans/server-route'
|
|
4
|
+
import { createBasicGuard } from './utils/helper.js'
|
|
5
|
+
import type { CommonEntrypoint } from '@owlmeans/entrypoint'
|
|
6
|
+
|
|
7
|
+
export const elevate = <R>(
|
|
8
|
+
entrypoints: (CommonEntrypoint | ServerEntrypoint<R>)[],
|
|
9
|
+
alias: string,
|
|
10
|
+
handler?: RefedEntrypointHandler<R> | boolean | EntrypointOptions<R>,
|
|
11
|
+
opts?: boolean | EntrypointOptions<R>
|
|
12
|
+
): ServerEntrypoint<R>[] => {
|
|
13
|
+
const idx = entrypoints.findIndex(({ route }) => route.route.alias === alias)
|
|
14
|
+
if (idx === -1) {
|
|
15
|
+
throw new SyntaxError(`Entrypoint with alias ${alias} not present`)
|
|
16
|
+
}
|
|
17
|
+
if (typeof handler === 'boolean') {
|
|
18
|
+
opts = handler
|
|
19
|
+
handler = undefined
|
|
20
|
+
}
|
|
21
|
+
if (typeof handler === 'object' && typeof handler !== 'function') {
|
|
22
|
+
opts = handler
|
|
23
|
+
handler = undefined
|
|
24
|
+
}
|
|
25
|
+
const force = typeof opts === 'object' && opts.force
|
|
26
|
+
if (isServerRouteModel(entrypoints[idx].route) && !force) {
|
|
27
|
+
throw new SyntaxError(`Entrypoint with alias ${alias} is already elevated`)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
entrypoints[idx] = entrypoint(
|
|
31
|
+
entrypoints[idx], handler, typeof opts === 'boolean' ? { intermediate: opts } : opts
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return entrypoints as ServerEntrypoint<R>[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const guard = <R>(guard: string, opts?: EntrypointOptions<R>): EntrypointOptions<R> =>
|
|
38
|
+
({ ...createBasicGuard(guard, opts) })
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ServerRouteModel, ServerRouteOptions } from '@owlmeans/server-route'
|
|
2
|
+
import type { CommonEntrypoint, EntrypointHandler, CommonEntrypointOptions } from '@owlmeans/entrypoint'
|
|
3
|
+
import type { Service } from '@owlmeans/context'
|
|
4
|
+
|
|
5
|
+
export interface ServerEntrypoint<R> extends CommonEntrypoint {
|
|
6
|
+
route: ServerRouteModel<R>
|
|
7
|
+
fixer?: string
|
|
8
|
+
handle: EntrypointHandler
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface EntrypointOptions<R> extends CommonEntrypointOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Force entrypoint to be elevated even if it is already elevated
|
|
14
|
+
*/
|
|
15
|
+
force?: boolean
|
|
16
|
+
fixer?: string
|
|
17
|
+
intermediate?: boolean
|
|
18
|
+
routeOptions?: ServerRouteOptions<R>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface FixerService extends Service {
|
|
22
|
+
handle: <R>(reply: R, error: Error) => void
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface EntrypointRef<R> {
|
|
26
|
+
ref?: ServerEntrypoint<R>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface RefedEntrypointHandler<R = {}> {
|
|
30
|
+
(ref: EntrypointRef<R>): EntrypointHandler
|
|
31
|
+
}
|