@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.
Files changed (60) hide show
  1. package/README.md +11 -10
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/client-entrypoint/SKILL.md +1 -1
  4. package/build/consts.d.ts +2 -0
  5. package/build/consts.d.ts.map +1 -0
  6. package/build/consts.js +2 -0
  7. package/build/consts.js.map +1 -0
  8. package/build/entrypoint.d.ts.map +1 -1
  9. package/build/entrypoint.js +6 -5
  10. package/build/entrypoint.js.map +1 -1
  11. package/build/failure.d.ts +2 -16
  12. package/build/failure.d.ts.map +1 -1
  13. package/build/failure.js +1 -1
  14. package/build/failure.js.map +1 -1
  15. package/build/helper/types.d.ts +10 -0
  16. package/build/helper/types.d.ts.map +1 -0
  17. package/build/helper/types.js +2 -0
  18. package/build/helper/types.js.map +1 -0
  19. package/build/helper.d.ts +4 -4
  20. package/build/helper.d.ts.map +1 -1
  21. package/build/helper.js +22 -19
  22. package/build/helper.js.map +1 -1
  23. package/build/index.d.ts +4 -1
  24. package/build/index.d.ts.map +1 -1
  25. package/build/index.js +3 -1
  26. package/build/index.js.map +1 -1
  27. package/build/stab.d.ts +4 -0
  28. package/build/stab.d.ts.map +1 -0
  29. package/build/stab.js +5 -0
  30. package/build/stab.js.map +1 -0
  31. package/build/types.d.ts +16 -2
  32. package/build/types.d.ts.map +1 -1
  33. package/build/utils/handler/types.d.ts +15 -0
  34. package/build/utils/handler/types.d.ts.map +1 -0
  35. package/build/utils/handler/types.js +2 -0
  36. package/build/utils/handler/types.js.map +1 -0
  37. package/build/utils/handler.d.ts +6 -4
  38. package/build/utils/handler.d.ts.map +1 -1
  39. package/build/utils/handler.js +120 -114
  40. package/build/utils/handler.js.map +1 -1
  41. package/build/utils/index.d.ts +1 -0
  42. package/build/utils/index.d.ts.map +1 -1
  43. package/build/utils/index.js.map +1 -1
  44. package/build/utils/types.local.d.ts +5 -0
  45. package/build/utils/types.local.d.ts.map +1 -0
  46. package/build/utils/types.local.js +2 -0
  47. package/build/utils/types.local.js.map +1 -0
  48. package/package.json +13 -13
  49. package/src/consts.ts +1 -0
  50. package/src/entrypoint.ts +6 -5
  51. package/src/failure.ts +3 -21
  52. package/src/helper/types.ts +10 -0
  53. package/src/helper.ts +28 -23
  54. package/src/index.ts +4 -1
  55. package/src/stab.ts +6 -0
  56. package/src/types.ts +18 -6
  57. package/src/utils/handler/types.ts +15 -0
  58. package/src/utils/handler.ts +81 -79
  59. package/src/utils/index.ts +1 -0
  60. package/src/utils/types.local.ts +5 -0
@@ -1,68 +1,62 @@
1
1
  import type { ApiClient } from '@owlmeans/api'
2
2
  import { DEFAULT_KEY } from '@owlmeans/client-config'
3
- import type { ClientConfig, ClientContext } from '@owlmeans/client-context'
4
- import type { AbstractRequest, EntrypointHandler, EntrypointTransport } from '@owlmeans/entrypoint'
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 { extractParams } from '@owlmeans/client-route'
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 '../failure.js'
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
- type Config = ClientConfig
20
- interface Context<C extends Config = Config> extends ClientContext<C> { }
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
- export const apiHandler: <
23
- T, R extends AbstractRequest = AbstractRequest
24
- >(ref: EntrypointRef<T, R>) => EntrypointHandler = (ref) => async (req, res) => {
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
- const ep = context.entrypoint<ClientEntrypoint>(req.alias)
29
+ const ep = context.entrypoint<ClientEntrypoint>(req.alias)
33
30
 
34
- // A route names the protocol it answers on. Whatever is bound under that protocol takes the
35
- // call, so a consumer writes `ep.call(...)` without learning which transport ran.
36
- const transport = transportAlias(ep.route.route.protocol)
37
- if (context.hasService(transport)) {
38
- req.path = ep.path()
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
- return context.service<EntrypointTransport>(transport).handle(req, res)
41
- }
37
+ return context.service<EntrypointTransport>(transport).handle(req, res)
38
+ }
42
39
 
43
- // Which web client carries this call is decided by the service the route answers on — the one it
44
- // names, or the one the context picks for its app type when it names none.
45
- const route = ep.service()
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
- const alias: string | undefined = typeof context.cfg.webService === 'string'
48
- ? context.cfg.webService
49
- : context.cfg.webService[route.service] ?? context.cfg.webService[DEFAULT_KEY]
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
- if (alias == null) {
52
- throw new SyntaxError(`Can't cast web service alias for ${ep.alias} entrypoint`)
53
- }
48
+ if (alias == null) {
49
+ throw new SyntaxError(`Can't cast web service alias for ${ep.alias} entrypoint`)
50
+ }
54
51
 
55
- const service: ApiClient = context.service(alias)
52
+ const service: ApiClient = context.service(alias)
56
53
 
57
- req.path = ep.path()
54
+ req.path = ep.path()
58
55
 
59
- return service.handler(req, res as any)
60
- }
56
+ return service.handler(req, res as any)
57
+ }
61
58
 
62
- export const apiInvoke: <
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(ref)(request, reply)
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
- export const entrypointUrl: <
129
- T, R extends ClientRequest = ClientRequest
130
- >(ref: EntrypointRef<T, R>, req?: Partial<R>, opts?: EntrypointUrlOptions) => Promise<string> = async (ref, req, opts) => {
131
- const ep = ref.ref
132
- if (ep == null) {
133
- throw new SyntaxError('Try to make URL before the entrypoint is created')
134
- }
135
- const ctx = ep.ctx
136
- if (ctx == null) {
137
- throw new SyntaxError(`No context provided in entrypointUrl for ${ep.alias} entrypoint`)
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
- // The base is left off here: an in-service URL is addressed relative to it, and the absolute
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 path
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)
@@ -1,3 +1,4 @@
1
1
 
2
+ export type * from './handler/types.js'
2
3
  export * from './entrypoint.js'
3
4
  export * from './handler.js'
@@ -0,0 +1,5 @@
1
+ import type { ClientConfig, ClientContext } from '@owlmeans/client-context'
2
+
3
+ export type Config = ClientConfig
4
+
5
+ export interface Context<C extends Config = Config> extends ClientContext<C> { }