@owlmeans/client-entrypoint 0.1.18-rc.45 → 0.1.18-rc.47
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 +11 -10
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/client-entrypoint/SKILL.md +1 -1
- package/build/consts.d.ts +2 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +2 -0
- package/build/consts.js.map +1 -0
- package/build/entrypoint.d.ts.map +1 -1
- package/build/entrypoint.js +6 -5
- package/build/entrypoint.js.map +1 -1
- package/build/failure.d.ts +2 -16
- package/build/failure.d.ts.map +1 -1
- package/build/failure.js +1 -1
- package/build/failure.js.map +1 -1
- package/build/helper/types.d.ts +10 -0
- package/build/helper/types.d.ts.map +1 -0
- package/build/helper/types.js +2 -0
- package/build/helper/types.js.map +1 -0
- package/build/helper.d.ts +4 -4
- package/build/helper.d.ts.map +1 -1
- package/build/helper.js +22 -19
- package/build/helper.js.map +1 -1
- package/build/index.d.ts +4 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +3 -1
- package/build/index.js.map +1 -1
- package/build/stab.d.ts +4 -0
- package/build/stab.d.ts.map +1 -0
- package/build/stab.js +5 -0
- package/build/stab.js.map +1 -0
- package/build/types.d.ts +16 -2
- package/build/types.d.ts.map +1 -1
- package/build/utils/handler/types.d.ts +15 -0
- package/build/utils/handler/types.d.ts.map +1 -0
- package/build/utils/handler/types.js +2 -0
- package/build/utils/handler/types.js.map +1 -0
- package/build/utils/handler.d.ts +6 -4
- package/build/utils/handler.d.ts.map +1 -1
- package/build/utils/handler.js +120 -114
- package/build/utils/handler.js.map +1 -1
- package/build/utils/index.d.ts +1 -0
- package/build/utils/index.d.ts.map +1 -1
- package/build/utils/index.js.map +1 -1
- package/build/utils/types.local.d.ts +5 -0
- package/build/utils/types.local.d.ts.map +1 -0
- package/build/utils/types.local.js +2 -0
- package/build/utils/types.local.js.map +1 -0
- package/package.json +13 -13
- package/src/consts.ts +1 -0
- package/src/entrypoint.ts +6 -5
- package/src/failure.ts +3 -21
- package/src/helper/types.ts +10 -0
- package/src/helper.ts +28 -23
- package/src/index.ts +4 -1
- package/src/stab.ts +6 -0
- package/src/types.ts +18 -6
- package/src/utils/handler/types.ts +15 -0
- package/src/utils/handler.ts +81 -79
- package/src/utils/index.ts +1 -0
- package/src/utils/types.local.ts +5 -0
package/src/utils/handler.ts
CHANGED
|
@@ -1,68 +1,62 @@
|
|
|
1
1
|
import type { ApiClient } from '@owlmeans/api'
|
|
2
2
|
import { DEFAULT_KEY } from '@owlmeans/client-config'
|
|
3
|
-
import type
|
|
4
|
-
import type {
|
|
5
|
-
import { EntrypointOutcome, provideResponse, transportAlias } from '@owlmeans/entrypoint'
|
|
6
|
-
import type { ClientEntrypoint, EntrypointInvoke, EntrypointUrlOptions, ClientEntrypointOptions, EntrypointRef, ClientRequest } from '../types.js'
|
|
3
|
+
import { type AbstractRequest, type EntrypointHandler, type EntrypointTransport, EntrypointOutcome, provideResponse, transportAlias } from '@owlmeans/entrypoint'
|
|
4
|
+
import type { ClientEntrypoint, EntrypointInvoke, EntrypointUrlOptions, ClientEntrypointOptions, EntrypointRef, ClientRequest, EntrypointFailureService } from '../types.js'
|
|
7
5
|
import { validate } from './entrypoint.js'
|
|
8
|
-
import {
|
|
6
|
+
import { clientRouteHelper } from '@owlmeans/client-route'
|
|
9
7
|
import { PARAM, RouteMethod, RouteProtocols } from '@owlmeans/route'
|
|
10
8
|
import { stringify } from 'qs'
|
|
11
|
-
import { assertContext } from '@owlmeans/context'
|
|
9
|
+
import { assertContext, memoHelper } from '@owlmeans/context'
|
|
12
10
|
import { makeSecurityHelper } from '@owlmeans/config'
|
|
13
|
-
import { ENTRYPOINT_FAILURE_SERVICE } from '../
|
|
14
|
-
import type { EntrypointFailureService } from '../failure.js'
|
|
11
|
+
import { ENTRYPOINT_FAILURE_SERVICE } from '../consts.js'
|
|
15
12
|
import { logger } from '@owlmeans/log'
|
|
13
|
+
import type { Config, Context } from './types.local.js'
|
|
14
|
+
import type { ApiCallHelper } from './handler/types.js'
|
|
16
15
|
|
|
17
16
|
const log = logger('client-entrypoint')
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
export const makeApiCallHelper = <T, R extends AbstractRequest = AbstractRequest>(
|
|
19
|
+
ref: EntrypointRef<T, R>
|
|
20
|
+
): ApiCallHelper<T, R> => {
|
|
21
|
+
const apiHandler: EntrypointHandler = async (req, res) => {
|
|
22
|
+
const location = `client-entrypoint:api-handler:${ref.ref?.alias}`
|
|
23
|
+
const context = assertContext<Config, Context>(ref.ref?.ctx as Context, location)
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const location = `client-entrypoint:api-handler:${ref.ref?.alias}`
|
|
26
|
-
const context = assertContext<Config, Context>(ref.ref?.ctx as Context, location)
|
|
27
|
-
|
|
28
|
-
if (context.cfg.webService == null) {
|
|
29
|
-
throw new SyntaxError('No webService provided')
|
|
30
|
-
}
|
|
25
|
+
if (context.cfg.webService == null) {
|
|
26
|
+
throw new SyntaxError('No webService provided')
|
|
27
|
+
}
|
|
31
28
|
|
|
32
|
-
|
|
29
|
+
const ep = context.entrypoint<ClientEntrypoint>(req.alias)
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
// A route names the protocol it answers on. Whatever is bound under that protocol takes the
|
|
32
|
+
// call, so a consumer writes `ep.call(...)` without learning which transport ran.
|
|
33
|
+
const transport = transportAlias(ep.route.route.protocol)
|
|
34
|
+
if (context.hasService(transport)) {
|
|
35
|
+
req.path = ep.path()
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
return context.service<EntrypointTransport>(transport).handle(req, res)
|
|
38
|
+
}
|
|
42
39
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
// Which web client carries this call is decided by the service the route answers on — the one it
|
|
41
|
+
// names, or the one the context picks for its app type when it names none.
|
|
42
|
+
const route = ep.service()
|
|
46
43
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
const alias: string | undefined = typeof context.cfg.webService === 'string'
|
|
45
|
+
? context.cfg.webService
|
|
46
|
+
: context.cfg.webService[route.service] ?? context.cfg.webService[DEFAULT_KEY]
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
if (alias == null) {
|
|
49
|
+
throw new SyntaxError(`Can't cast web service alias for ${ep.alias} entrypoint`)
|
|
50
|
+
}
|
|
54
51
|
|
|
55
|
-
|
|
52
|
+
const service: ApiClient = context.service(alias)
|
|
56
53
|
|
|
57
|
-
|
|
54
|
+
req.path = ep.path()
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
}
|
|
56
|
+
return service.handler(req, res as any)
|
|
57
|
+
}
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
T, R extends AbstractRequest = AbstractRequest
|
|
64
|
-
>(ref: EntrypointRef<T, R>, opts?: ClientEntrypointOptions) => EntrypointInvoke<T, R> =
|
|
65
|
-
(ref, opts) => (async (req) => {
|
|
59
|
+
const apiInvoke = (opts?: ClientEntrypointOptions): EntrypointInvoke<T, R> => (async (req: Partial<R> | undefined) => {
|
|
66
60
|
const ep = ref.ref
|
|
67
61
|
if (ep == null) {
|
|
68
62
|
throw new SyntaxError('Try to make API call before the entrypoint is created')
|
|
@@ -112,7 +106,7 @@ export const apiInvoke: <
|
|
|
112
106
|
throw new SyntaxError(`Use entrypoint ${ep.alias} without context`)
|
|
113
107
|
}
|
|
114
108
|
try {
|
|
115
|
-
await apiHandler(
|
|
109
|
+
await apiHandler(request, reply)
|
|
116
110
|
if (reply.error != null) throw reply.error
|
|
117
111
|
} catch (error) {
|
|
118
112
|
if (ctx.hasService(ENTRYPOINT_FAILURE_SERVICE)) {
|
|
@@ -125,41 +119,49 @@ export const apiInvoke: <
|
|
|
125
119
|
return { value: reply.value ?? null, outcome: reply.outcome ?? EntrypointOutcome.Ok }
|
|
126
120
|
}) as EntrypointInvoke<any>
|
|
127
121
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
122
|
+
const entrypointUrl = async (req?: Partial<R>, opts?: EntrypointUrlOptions): Promise<string> => {
|
|
123
|
+
const ep = ref.ref
|
|
124
|
+
if (ep == null) {
|
|
125
|
+
throw new SyntaxError('Try to make URL before the entrypoint is created')
|
|
126
|
+
}
|
|
127
|
+
const ctx = ep.ctx
|
|
128
|
+
if (ctx == null) {
|
|
129
|
+
throw new SyntaxError(`No context provided in entrypointUrl for ${ep.alias} entrypoint`)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// The base is left off here: an in-service URL is addressed relative to it, and the absolute
|
|
133
|
+
// branch below has `makeUrl` prepend it.
|
|
134
|
+
const epPath = ep.path()
|
|
135
|
+
const pathParams = clientRouteHelper.extractParams(epPath)
|
|
136
|
+
let path = pathParams.reduce((p, param) => {
|
|
137
|
+
return p.replace(`${PARAM}${param}`, `${req?.params?.[param as keyof typeof req.params]}`)
|
|
138
|
+
}, epPath) + (req?.query != null ? `?${stringify(req?.query)}` : '')
|
|
139
|
+
|
|
140
|
+
// A socket address is always absolute, whoever owns the route. `new WebSocket(path)` resolves a
|
|
141
|
+
// relative value against the PAGE's origin, which in a split deployment is the web host and not
|
|
142
|
+
// the service that answers the upgrade — so the handshake goes somewhere that never speaks it
|
|
143
|
+
// and dies on a gateway timeout. Only a same-origin HTTP route benefits from staying relative.
|
|
144
|
+
const socket = ep.route.route.protocol === RouteProtocols.SOCKET
|
|
145
|
+
|
|
146
|
+
// Locality is a question about the RESOLVED service, not about what the declaration happens to
|
|
147
|
+
// name: a route that names no service belongs to the asking context, and addressing it absolutely
|
|
148
|
+
// would turn in-app navigation into a full page load.
|
|
149
|
+
if (socket || !ep.isLocal() || opts?.absolute === true) {
|
|
150
|
+
const helper = makeSecurityHelper(ctx)
|
|
151
|
+
path = helper.makeUrl(
|
|
152
|
+
ep.address(), path, { host: req?.host, base: req?.base, forceUnsecure: req?.unsecure }
|
|
153
|
+
)
|
|
154
|
+
}
|
|
139
155
|
|
|
140
|
-
|
|
141
|
-
// branch below has `makeUrl` prepend it.
|
|
142
|
-
const epPath = ep.path()
|
|
143
|
-
const pathParams = extractParams(epPath)
|
|
144
|
-
let path = pathParams.reduce((p, param) => {
|
|
145
|
-
return p.replace(`${PARAM}${param}`, `${req?.params?.[param as keyof typeof req.params]}`)
|
|
146
|
-
}, epPath) + (req?.query != null ? `?${stringify(req?.query)}` : '')
|
|
147
|
-
|
|
148
|
-
// A socket address is always absolute, whoever owns the route. `new WebSocket(path)` resolves a
|
|
149
|
-
// relative value against the PAGE's origin, which in a split deployment is the web host and not
|
|
150
|
-
// the service that answers the upgrade — so the handshake goes somewhere that never speaks it
|
|
151
|
-
// and dies on a gateway timeout. Only a same-origin HTTP route benefits from staying relative.
|
|
152
|
-
const socket = ep.route.route.protocol === RouteProtocols.SOCKET
|
|
153
|
-
|
|
154
|
-
// Locality is a question about the RESOLVED service, not about what the declaration happens to
|
|
155
|
-
// name: a route that names no service belongs to the asking context, and addressing it absolutely
|
|
156
|
-
// would turn in-app navigation into a full page load.
|
|
157
|
-
if (socket || !ep.isLocal() || opts?.absolute === true) {
|
|
158
|
-
const helper = makeSecurityHelper(ctx)
|
|
159
|
-
path = helper.makeUrl(
|
|
160
|
-
ep.address(), path, { host: req?.host, base: req?.base, forceUnsecure: req?.unsecure }
|
|
161
|
-
)
|
|
156
|
+
return path
|
|
162
157
|
}
|
|
163
158
|
|
|
164
|
-
return
|
|
159
|
+
return { apiHandler, apiInvoke, entrypointUrl }
|
|
165
160
|
}
|
|
161
|
+
|
|
162
|
+
export const apiCallOf = memoHelper.oncePer(makeApiCallHelper)
|
|
163
|
+
|
|
164
|
+
/** @deprecated compat:factory-refactor — use `makeApiCallHelper(ref).entrypointUrl(…)` */
|
|
165
|
+
export const entrypointUrl = <T, R extends ClientRequest = ClientRequest>(
|
|
166
|
+
ref: EntrypointRef<T, R>, req?: Partial<R>, opts?: EntrypointUrlOptions
|
|
167
|
+
): Promise<string> => makeApiCallHelper(ref).entrypointUrl(req, opts)
|
package/src/utils/index.ts
CHANGED