@owlmeans/api 0.1.18-rc.46 → 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 +2 -2
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/api/SKILL.md +22 -20
  4. package/build/index.d.ts +2 -2
  5. package/build/index.d.ts.map +1 -1
  6. package/build/index.js +1 -1
  7. package/build/index.js.map +1 -1
  8. package/build/service.js +6 -6
  9. package/build/service.js.map +1 -1
  10. package/build/status/helper.d.ts +4 -0
  11. package/build/status/helper.d.ts.map +1 -0
  12. package/build/status/helper.js +49 -0
  13. package/build/status/helper.js.map +1 -0
  14. package/build/status/index.d.ts +3 -22
  15. package/build/status/index.d.ts.map +1 -1
  16. package/build/status/index.js +4 -61
  17. package/build/status/index.js.map +1 -1
  18. package/build/status/types.d.ts +24 -0
  19. package/build/status/types.d.ts.map +1 -1
  20. package/build/utils/body/types.d.ts +36 -0
  21. package/build/utils/body/types.d.ts.map +1 -0
  22. package/build/utils/{types.js.map → body/types.js.map} +1 -1
  23. package/build/utils/body.d.ts +3 -38
  24. package/build/utils/body.d.ts.map +1 -1
  25. package/build/utils/body.js +49 -66
  26. package/build/utils/body.js.map +1 -1
  27. package/build/utils/index.d.ts +2 -1
  28. package/build/utils/index.d.ts.map +1 -1
  29. package/build/utils/index.js +1 -1
  30. package/build/utils/index.js.map +1 -1
  31. package/build/utils/response/types.d.ts +15 -0
  32. package/build/utils/response/types.d.ts.map +1 -0
  33. package/build/utils/response/types.js +2 -0
  34. package/build/utils/response/types.js.map +1 -0
  35. package/build/utils/response.d.ts +4 -0
  36. package/build/utils/response.d.ts.map +1 -0
  37. package/build/utils/response.js +99 -0
  38. package/build/utils/response.js.map +1 -0
  39. package/package.json +11 -11
  40. package/src/index.ts +2 -2
  41. package/src/service.ts +6 -6
  42. package/src/status/helper.ts +63 -0
  43. package/src/status/index.ts +5 -76
  44. package/src/status/types.ts +25 -0
  45. package/src/utils/body/types.ts +37 -0
  46. package/src/utils/body.ts +50 -67
  47. package/src/utils/index.ts +2 -1
  48. package/src/utils/response/types.ts +15 -0
  49. package/src/utils/response.ts +117 -0
  50. package/tests/body.spec.ts +29 -29
  51. package/tests/handler.spec.ts +45 -45
  52. package/build/utils/handler.d.ts +0 -11
  53. package/build/utils/handler.d.ts.map +0 -1
  54. package/build/utils/handler.js +0 -95
  55. package/build/utils/handler.js.map +0 -1
  56. package/build/utils/types.d.ts +0 -11
  57. package/build/utils/types.d.ts.map +0 -1
  58. package/src/utils/handler.ts +0 -110
  59. package/src/utils/types.ts +0 -12
  60. /package/build/utils/{types.js → body/types.js} +0 -0
@@ -2,72 +2,55 @@ import qs from 'qs';
2
2
  import { RouteMethod } from '@owlmeans/route';
3
3
  import { FORM_CONTENT_TYPE } from './consts.local.js';
4
4
  import { JSON_CONTENT_TYPE } from './consts.js';
5
- /** The request's own `content-type`, whatever case its key is written in. */
6
- const contentTypeOf = (headers) => {
7
- const value = Object.entries(headers ?? {}).find(([key]) => key.toLowerCase() === 'content-type')?.[1];
8
- if (value == null || value === '') {
9
- return undefined;
10
- }
11
- return Array.isArray(value) ? value.join(', ') : `${value}`;
12
- };
13
- /** `application/json` or a `+json` suffix type, with or without parameters. */
14
- const isJsonContentType = (type) => type != null && /^application\/(?:[^\s;]+\+)?json\s*(?:;|$)/i.test(type.trim());
15
- /** A JSON value no serializer quotes for us: a string, a number or a boolean. */
16
- const isScalar = (value) => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
17
- /** Does the entrypoint's body schema declare a string? */
18
- const stringContract = (schema) => {
19
- const type = schema?.type;
20
- return (Array.isArray(type) ? type : [type]).includes('string');
21
- };
22
- const parse = (text) => {
23
- try {
24
- return { json: true, value: JSON.parse(text) };
25
- }
26
- catch {
27
- return { json: false };
28
- }
29
- };
30
- /**
31
- * The JSON text a scalar body travels as, so the server parses it back to the same value.
32
- *
33
- * A number or a boolean is `JSON.stringify`'d. A string is a value unless it is already JSON text:
34
- * one that does not parse is quoted; one that parses is sent as it is (the caller serialized it
35
- * itself) — except under a body schema of `type: 'string'`, where only a JSON string literal
36
- * counts as serialized and `123`, `true` or `{…}` are the text the caller means.
37
- */
38
- const jsonScalarBody = (body, schema) => {
39
- if (typeof body !== 'string') {
40
- return JSON.stringify(body);
41
- }
42
- const parsed = parse(body);
43
- const serialized = parsed.json && (!stringContract(schema) || typeof parsed.value === 'string');
44
- return serialized ? body : JSON.stringify(body);
45
- };
46
- /**
47
- * What a request's body becomes on the wire.
48
- *
49
- * - A `x-www-form-urlencoded` request: the body through `qs`.
50
- * - A string, number or boolean under a JSON `content-type` — the caller's, or `application/json`
51
- * supplied for a `POST` that names none — becomes JSON text ({@link jsonScalarBody}).
52
- * - Anything else — objects, arrays, a scalar under another content type — goes to axios as it is,
53
- * which serializes an object or an array as JSON.
54
- */
55
- const requestBodyOf = (body, headers, method, schema) => {
56
- const type = contentTypeOf(headers);
57
- if (body != null && type?.includes(FORM_CONTENT_TYPE) === true) {
58
- return { data: qs.stringify(body), verbatim: false };
59
- }
60
- if (!isScalar(body)) {
5
+ export const createBodyUtils = () => {
6
+ const contentTypeOf = (headers) => {
7
+ const value = Object.entries(headers ?? {}).find(([key]) => key.toLowerCase() === 'content-type')?.[1];
8
+ if (value == null || value === '') {
9
+ return undefined;
10
+ }
11
+ return Array.isArray(value) ? value.join(', ') : `${value}`;
12
+ };
13
+ const isJsonContentType = (type) => type != null && /^application\/(?:[^\s;]+\+)?json\s*(?:;|$)/i.test(type.trim());
14
+ /** A JSON value no serializer quotes for us: a string, a number or a boolean. */
15
+ const isScalar = (value) => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
16
+ /** Does the entrypoint's body schema declare a string? */
17
+ const stringContract = (schema) => {
18
+ const type = schema?.type;
19
+ return (Array.isArray(type) ? type : [type]).includes('string');
20
+ };
21
+ const parse = (text) => {
22
+ try {
23
+ return { json: true, value: JSON.parse(text) };
24
+ }
25
+ catch {
26
+ return { json: false };
27
+ }
28
+ };
29
+ const jsonScalarBody = (body, schema) => {
30
+ if (typeof body !== 'string') {
31
+ return JSON.stringify(body);
32
+ }
33
+ const parsed = parse(body);
34
+ const serialized = parsed.json && (!stringContract(schema) || typeof parsed.value === 'string');
35
+ return serialized ? body : JSON.stringify(body);
36
+ };
37
+ const requestBodyOf = (body, headers, method, schema) => {
38
+ const type = contentTypeOf(headers);
39
+ if (body != null && type?.includes(FORM_CONTENT_TYPE) === true) {
40
+ return { data: qs.stringify(body), verbatim: false };
41
+ }
42
+ if (!isScalar(body)) {
43
+ return { data: body, verbatim: false };
44
+ }
45
+ if (type == null && method === RouteMethod.POST) {
46
+ return { data: jsonScalarBody(body, schema), contentType: JSON_CONTENT_TYPE, verbatim: true };
47
+ }
48
+ if (isJsonContentType(type)) {
49
+ return { data: jsonScalarBody(body, schema), verbatim: true };
50
+ }
61
51
  return { data: body, verbatim: false };
62
- }
63
- if (type == null && method === RouteMethod.POST) {
64
- return { data: jsonScalarBody(body, schema), contentType: JSON_CONTENT_TYPE, verbatim: true };
65
- }
66
- if (isJsonContentType(type)) {
67
- return { data: jsonScalarBody(body, schema), verbatim: true };
68
- }
69
- return { data: body, verbatim: false };
52
+ };
53
+ return { contentTypeOf, isJsonContentType, jsonScalarBody, requestBodyOf };
70
54
  };
71
- export const createBodyHelper = () => ({ contentTypeOf, isJsonContentType, jsonScalarBody, requestBodyOf });
72
- export const bodyHelper = createBodyHelper();
55
+ export const bodyUtils = createBodyUtils();
73
56
  //# sourceMappingURL=body.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"body.js","sourceRoot":"","sources":["../../src/utils/body.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,EAAE,iBAAiB,EAAE,MAAM,aA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E,QAAQ,EAAE,IAAI,EAAE,CAAA;QAC/D,CAAC;QAED,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAA;IACxC,CAAC,CAAA;IAED,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,cAAc,EAAE,aAAa,EAAE,CAAA;AAC5E,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,eAAe,EAAE,CAAA"}
@@ -1,2 +1,3 @@
1
- export * from './handler.js';
1
+ export type * from './response/types.js';
2
+ export * from './response.js';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,mBAAmB,qBAAqB,CAAA;AACxC,cAAc,eAAe,CAAA"}
@@ -1,2 +1,2 @@
1
- export * from './handler.js';
1
+ export * from './response.js';
2
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,cAAc,eAAe,CAAA"}
@@ -0,0 +1,15 @@
1
+ import type { AbstractResponse } from '@owlmeans/entrypoint';
2
+ import type { AxiosResponse } from 'axios';
3
+ import type { ApiClientError } from '../../errors.js';
4
+ /** Turns an axios answer into the entrypoint reply: a resolved value or a typed failure. */
5
+ export interface ResponseUtils {
6
+ /** Resolves `reply` with a 2xx answer's value, or rejects it with the failure of any other status. */
7
+ processResponse: (response: AxiosResponse, reply: AbstractResponse<any>) => void;
8
+ /**
9
+ * The typed error of a bare status: `ServerCrashedError` (500), `ServerAuthError` (401),
10
+ * `ApiClientError('forbidden[:<id>]')` (403), `ApiStatusError` (`api:client:status:<n>[:<id>]`)
11
+ * for every other one.
12
+ */
13
+ statusError: (status: number, incidentId?: string) => ApiClientError;
14
+ }
15
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/utils/response/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAErD,4FAA4F;AAC5F,MAAM,WAAW,aAAa;IAC5B,sGAAsG;IACtG,eAAe,EAAE,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,gBAAgB,CAAC,GAAG,CAAC,KAAK,IAAI,CAAA;IAChF;;;;OAIG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,cAAc,CAAA;CACrE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/response/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import type { ResponseUtils } from './response/types.js';
2
+ export declare const createResponseUtils: () => ResponseUtils;
3
+ export declare const responseUtils: ResponseUtils;
4
+ //# sourceMappingURL=response.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/utils/response.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAExD,eAAO,MAAM,mBAAmB,QAAO,aAqGtC,CAAA;AAED,eAAO,MAAM,aAAa,eAAwB,CAAA"}
@@ -0,0 +1,99 @@
1
+ import { EntrypointOutcome } from '@owlmeans/entrypoint';
2
+ import { ACCEPTED, CREATED, FINISHED, FORBIDDEN_ERROR, INCIDENT_ID_HEADER, DENIAL_KIND_HEADER, ACCESS_DENIED_KIND, OK, SERVER_ERROR, UNAUTHORIZED_ERROR, } from '../consts.js';
3
+ import { ResilientError } from '@owlmeans/error';
4
+ import { ApiClientError, ApiStatusError, ServerAuthError, ServerCrashedError } from '../errors.js';
5
+ import { apiStatusHelper } from '../status/helper.js';
6
+ import { logger } from '@owlmeans/log';
7
+ export const createResponseUtils = () => {
8
+ const log = logger('api');
9
+ const processResponse = (response, reply) => {
10
+ switch (response.status) {
11
+ case OK:
12
+ reply.resolve(response.data, EntrypointOutcome.Ok);
13
+ return;
14
+ case ACCEPTED:
15
+ reply.resolve(response.data, EntrypointOutcome.Accepted);
16
+ return;
17
+ case CREATED:
18
+ return processEmptyResponse(response, reply, EntrypointOutcome.Created);
19
+ case FINISHED:
20
+ return processEmptyResponse(response, reply, EntrypointOutcome.Finished);
21
+ default:
22
+ // A refusal is ordinary traffic (401, 403, 409, 428): debug, and never the headers.
23
+ log.debug('Request answered with a failure', {
24
+ status: response.status, statusText: response.statusText, url: response.config?.url,
25
+ incidentId: headerOf(response.headers, INCIDENT_ID_HEADER),
26
+ });
27
+ reply.reject(failureOf(response));
28
+ }
29
+ };
30
+ /**
31
+ * The error a non-2xx answer rejects with — always carrying the HTTP status and, when the server
32
+ * sent one, its incident id.
33
+ *
34
+ * A development body is the marshaled `ResilientError`: it is rebuilt into its own class and
35
+ * stamped with `responseStatus`. Anything else — the bare incident id of a production body, a
36
+ * proxy's HTML, a framework's JSON — becomes the status error of {@link statusError}.
37
+ */
38
+ const failureOf = (response) => {
39
+ const incidentId = headerOf(response.headers, INCIDENT_ID_HEADER)
40
+ ?? (apiStatusHelper.isIncidentBody(response.data) ? response.data.trim() : undefined);
41
+ const denialKind = response.status === FORBIDDEN_ERROR
42
+ && headerOf(response.headers, DENIAL_KIND_HEADER) === ACCESS_DENIED_KIND
43
+ ? ACCESS_DENIED_KIND : undefined;
44
+ if (typeof response.data === 'string' && response.data.includes(ResilientError.separator)) {
45
+ try {
46
+ const error = ResilientError.ensure(response.data, true);
47
+ error.incidentId ??= incidentId;
48
+ Object.assign(error, {
49
+ responseStatus: response.status, denialKind,
50
+ });
51
+ return error;
52
+ }
53
+ catch {
54
+ log.warn('Server returned an unrecognizable text error', { status: response.status, body: response.data });
55
+ }
56
+ }
57
+ return Object.assign(statusError(response.status, incidentId), { denialKind });
58
+ };
59
+ /**
60
+ * The typed error of a bare status: `ServerCrashedError` (500), `ServerAuthError` (401),
61
+ * `ApiClientError('forbidden[:<id>]')` (403), `ApiStatusError` (`api:client:status:<n>[:<id>]`)
62
+ * for every other one.
63
+ */
64
+ const statusError = (status, incidentId) => {
65
+ const error = status === SERVER_ERROR ? new ServerCrashedError(incidentId)
66
+ : status === UNAUTHORIZED_ERROR ? new ServerAuthError(incidentId)
67
+ : status === FORBIDDEN_ERROR ? new ApiClientError(incidentId != null ? `forbidden:${incidentId}` : 'forbidden')
68
+ : new ApiStatusError(status, incidentId);
69
+ if (incidentId != null) {
70
+ error.incidentId = incidentId;
71
+ }
72
+ return Object.assign(error, { responseStatus: status });
73
+ };
74
+ /** One response header, from an `AxiosHeaders` instance or a plain header object alike. */
75
+ const headerOf = (headers, name) => {
76
+ if (headers == null || typeof headers !== 'object') {
77
+ return undefined;
78
+ }
79
+ const get = headers.get;
80
+ const viaGet = typeof get === 'function' ? get.call(headers, name) : undefined;
81
+ const value = viaGet ?? Object.entries(headers).find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
82
+ const first = Array.isArray(value) ? value[0] : value;
83
+ return typeof first === 'string' && first.trim() !== '' ? first.trim() : undefined;
84
+ };
85
+ const processEmptyResponse = (response, reply, outcome) => {
86
+ if (response.data != null && response.data != '') {
87
+ reply.resolve(response.data, outcome);
88
+ return;
89
+ }
90
+ if (response.headers.toJSON != null && typeof response.headers.toJSON === 'function') {
91
+ reply.resolve(response.headers.toJSON(), outcome);
92
+ return;
93
+ }
94
+ reply.resolve(response.headers, outcome);
95
+ };
96
+ return { processResponse, statusError };
97
+ };
98
+ export const responseUtils = createResponseUtils();
99
+ //# sourceMappingURL=response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.js","sourceRoot":"","sources":["../../src/utils/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAE/E,OAAO,EACL,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,kBAAkB,EAAE,kBAAkB,EACpF,kBAAkB,EAAE,EAAE,EAAE,YAAY,EAAE,kBAAkB,GACzD,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAClG,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErD,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAGtC,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAkB,EAAE;IACrD,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;IAEzB,MAAM,eAAe,GAAG,CAAC,QAAuB,EAAE,KAA4B,EAAQ,EAAE;QACtF,QAAQ,QAAQ,CAAC,MAAM,EAAE,CAAC;YACxB,KAAK,EAAE;gBACL,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC,EAAE,CAAC,CAAA;gBAClD,OAAM;YACR,KAAK,QAAQ;gBACX,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAA;gBACxD,OAAM;YACR,KAAK,OAAO;gBACV,OAAO,oBAAoB,CAAC,QAAQ,EAAE,KAAK,EAAE,iBAAiB,CAAC,OAAO,CAA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KAAK,UAAU,EAAE,CAAC;YACrF,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,OAAO,CAAC,CAAA;YACjD,OAAM;QACR,CAAC;QACD,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IAC1C,CAAC,CAAA;IAED,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,CAAA;AACzC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,mBAAmB,EAAE,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/api",
3
- "version": "0.1.18-rc.46",
3
+ "version": "0.1.18-rc.47",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -29,21 +29,21 @@
29
29
  }
30
30
  },
31
31
  "dependencies": {
32
- "@owlmeans/auth-common": "^0.1.18-rc.46",
33
- "@owlmeans/client-config": "^0.1.18-rc.43",
34
- "@owlmeans/client-route": "^0.1.18-rc.47",
35
- "@owlmeans/config": "^0.1.18-rc.43",
36
- "@owlmeans/context": "^0.1.18-rc.33",
37
- "@owlmeans/entrypoint": "^0.1.18-rc.40",
38
- "@owlmeans/error": "^0.1.18-rc.37",
39
- "@owlmeans/log": "^0.1.18-rc.1",
40
- "@owlmeans/route": "^0.1.18-rc.34",
32
+ "@owlmeans/auth-common": "^0.1.18-rc.47",
33
+ "@owlmeans/client-config": "^0.1.18-rc.44",
34
+ "@owlmeans/client-route": "^0.1.18-rc.48",
35
+ "@owlmeans/config": "^0.1.18-rc.44",
36
+ "@owlmeans/context": "^0.1.18-rc.34",
37
+ "@owlmeans/entrypoint": "^0.1.18-rc.41",
38
+ "@owlmeans/error": "^0.1.18-rc.38",
39
+ "@owlmeans/log": "^0.1.18-rc.2",
40
+ "@owlmeans/route": "^0.1.18-rc.35",
41
41
  "axios": "^1.20.0",
42
42
  "qs": "^6.16.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@owlmeans/dep-config": "workspace:*",
46
- "@owlmeans/resource": "^0.1.18-rc.39",
46
+ "@owlmeans/resource": "^0.1.18-rc.40",
47
47
  "@types/qs": "^6.15.1",
48
48
  "nodemon": "^3.1.14",
49
49
  "typescript": "^7.0.2"
package/src/index.ts CHANGED
@@ -3,7 +3,7 @@ export * from './consts.js'
3
3
  export * from './service.js'
4
4
  export type * from './types.js'
5
5
  export {
6
- httpStatusOf, incidentIdOf, isIncidentBody, parseClientMarker, API_CLIENT_MARKER, API_STATUS_MARKER,
6
+ httpStatusOf, apiStatusHelper, createApiStatusHelper, parseClientMarker, API_CLIENT_MARKER, API_STATUS_MARKER,
7
7
  } from './status/index.js'
8
- export type { ClientMarker } from './status/index.js'
8
+ export type { ClientMarker, ApiStatusHelper } from './status/index.js'
9
9
  export type { ResponseStatusCarrier } from './status/types.js'
package/src/service.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { assertContext, createService } from '@owlmeans/context'
2
- import { extractParams } from '@owlmeans/client-route'
2
+ import { clientRouteHelper } from '@owlmeans/client-route'
3
3
  import type { ApiClient } from './types.js'
4
4
  import axios, { AxiosHeaders } from 'axios'
5
5
  import type { AxiosRequestTransformer } from 'axios'
6
6
  import type { CommonEntrypoint } from '@owlmeans/entrypoint'
7
7
  import { DEFAULT_ALIAS, UNAUTHORIZED_ERROR } from './consts.js'
8
- import { processResponse } from './utils/handler.js'
8
+ import { responseUtils } from './utils/response.js'
9
9
  import { makeSecurityHelper } from '@owlmeans/config'
10
10
  import { AUTH_HEADER, DEF_AUTH_SRV, TOKEN_UPDATE, type AuthService } from '@owlmeans/auth-common'
11
11
  import type { Config, Context } from './types.local.js'
12
- import { bodyHelper } from './utils/body.js'
12
+ import { bodyUtils } from './utils/body.js'
13
13
 
14
14
  /** Whether a request carried an authentication header of its own. */
15
15
  const presented = (headers?: Record<string, unknown>): boolean =>
@@ -28,7 +28,7 @@ export const createApiService = (alias: string = DEFAULT_ALIAS): ApiClient => {
28
28
  const module = context.entrypoint<CommonEntrypoint>(request.alias)
29
29
  const route = module.route.route
30
30
  let path = module.path()
31
- const params = extractParams(path)
31
+ const params = clientRouteHelper.extractParams(path)
32
32
  path = params.reduce((path, param) => {
33
33
  type Key = keyof typeof request.params
34
34
  if (request.params[param as Key] == null) {
@@ -48,7 +48,7 @@ export const createApiService = (alias: string = DEFAULT_ALIAS): ApiClient => {
48
48
 
49
49
  // A scalar JSON body (a string, a number, a boolean) is serialized here, so the server parses
50
50
  // back the value the caller sent; objects and arrays are left to axios.
51
- const body = bodyHelper.requestBodyOf(request.body, request.headers, route.method, module.filter?.body)
51
+ const body = bodyUtils.requestBodyOf(request.body, request.headers, route.method, module.filter?.body)
52
52
  const transformer: AxiosRequestTransformer | undefined = body.verbatim ? data => data : undefined
53
53
  const requestHeaders = body.contentType != null
54
54
  ? { ...request.headers, 'content-type': body.contentType }
@@ -94,7 +94,7 @@ export const createApiService = (alias: string = DEFAULT_ALIAS): ApiClient => {
94
94
  await context.service<AuthService>(DEF_AUTH_SRV).update(undefined)
95
95
  }
96
96
 
97
- processResponse(response, reply)
97
+ responseUtils.processResponse(response, reply)
98
98
 
99
99
  return [reply.error ?? reply.value, reply.outcome] as any
100
100
  }
@@ -0,0 +1,63 @@
1
+ import { ResilientError } from '@owlmeans/error'
2
+ import { ApiClientError } from '../errors.js'
3
+ import { parseClientMarker } from './marker.js'
4
+ import { ACCESS_DENIED_KIND } from '../consts.js'
5
+ import { UUID } from './consts.local.js'
6
+ import type { ApiStatusHelper, ClientMarker, ResponseStatusCarrier } from './types.js'
7
+
8
+ export const createApiStatusHelper = (): ApiStatusHelper => {
9
+ const isAccessDenied = (error: unknown): boolean =>
10
+ httpStatusOf(error) === 403
11
+ && typeof error === 'object' && error != null
12
+ && (error as ResponseStatusCarrier).denialKind === ACCESS_DENIED_KIND
13
+
14
+ const httpStatus = (value: unknown): number | null =>
15
+ typeof value === 'number' && Number.isInteger(value) && value >= 100 && value <= 599 ? value : null
16
+
17
+ /** The first `api:client:*` marker in an error's `message`, else in its `type`. */
18
+ const markerOf = (error: object): ClientMarker | null => {
19
+ const { message, type } = error as { message?: unknown, type?: unknown }
20
+
21
+ return (typeof message === 'string' ? parseClientMarker(message) : null)
22
+ ?? (typeof type === 'string' ? parseClientMarker(type) : null)
23
+ }
24
+
25
+ const httpStatusOf = (error: unknown): number | null => {
26
+ if (error == null || typeof error !== 'object') {
27
+ return null
28
+ }
29
+ const stamped = httpStatus((error as ResponseStatusCarrier).responseStatus)
30
+ if (stamped != null) {
31
+ return stamped
32
+ }
33
+ if (error instanceof ApiClientError && httpStatus(error.status) != null) {
34
+ return error.status!
35
+ }
36
+ const declaration = error.constructor as { httpStatus?: unknown, allowServerErrorStatus?: unknown } | undefined
37
+ const declared = httpStatus(declaration?.httpStatus)
38
+ if (declared != null && (declared < 500 || declaration?.allowServerErrorStatus === true)) {
39
+ return declared
40
+ }
41
+
42
+ return markerOf(error)?.status ?? null
43
+ }
44
+
45
+ const incidentIdOf = (error: unknown): string | null => {
46
+ if (error == null || typeof error !== 'object') {
47
+ return null
48
+ }
49
+ const own = (error as { incidentId?: unknown }).incidentId
50
+ if (typeof own === 'string' && own !== '') {
51
+ return own
52
+ }
53
+
54
+ return markerOf(error)?.incidentId ?? null
55
+ }
56
+
57
+ const isIncidentBody = (data: unknown): data is string =>
58
+ typeof data === 'string' && !data.includes(ResilientError.separator) && UUID.test(data.trim())
59
+
60
+ return { isAccessDenied, httpStatusOf, incidentIdOf, isIncidentBody }
61
+ }
62
+
63
+ export const apiStatusHelper = createApiStatusHelper()
@@ -1,82 +1,11 @@
1
- import { ResilientError } from '@owlmeans/error'
2
- import { ApiClientError } from '../errors.js'
3
- import { parseClientMarker } from './marker.js'
1
+ import { apiStatusHelper } from './helper.js'
4
2
 
5
3
  export { INCIDENT_ID_HEADER } from '../consts.js'
6
4
  export { DENIAL_KIND_HEADER, ACCESS_DENIED_KIND } from '../consts.js'
7
- import { ACCESS_DENIED_KIND } from '../consts.js'
8
- import { UUID } from './consts.local.js'
9
- import type { ResponseStatusCarrier } from './types.js'
10
5
  export { parseClientMarker } from './marker.js'
11
6
  export { API_CLIENT_MARKER, API_STATUS_MARKER } from './consts.js'
12
- export type { ClientMarker } from './types.js'
7
+ export type { ClientMarker, ResponseStatusCarrier, ApiStatusHelper } from './types.js'
8
+ export { apiStatusHelper, createApiStatusHelper } from './helper.js'
13
9
 
14
- /** True only for a server-labelled auth/IAM refusal, never an unrelated 403. */
15
- export const isAccessDenied = (error: unknown): boolean =>
16
- httpStatusOf(error) === 403
17
- && typeof error === 'object' && error != null
18
- && (error as ResponseStatusCarrier).denialKind === ACCESS_DENIED_KIND
19
-
20
- const httpStatus = (value: unknown): number | null =>
21
- typeof value === 'number' && Number.isInteger(value) && value >= 100 && value <= 599 ? value : null
22
-
23
- /** The first `api:client:*` marker in an error's `message`, else in its `type`. */
24
- const markerOf = (error: object) => {
25
- const { message, type } = error as { message?: unknown, type?: unknown }
26
-
27
- return (typeof message === 'string' ? parseClientMarker(message) : null)
28
- ?? (typeof type === 'string' ? parseClientMarker(type) : null)
29
- }
30
-
31
- /**
32
- * The HTTP status an error answers to, or `null` when nothing states one.
33
- *
34
- * Read in order: the status the API client stamped on the error it rebuilt from a response
35
- * (`responseStatus`); the status an `ApiClientError` parsed from its marker; the `httpStatus` its
36
- * class declares — a 4xx always, a 5xx only with `allowServerErrorStatus`, exactly what an
37
- * `@owlmeans/server-api` boundary answers with; finally an `api:client:*` marker in the message or
38
- * the type of anything else. This is what lets a browser recognise a refusal from its status
39
- * alone when a production body carries nothing but an incident id.
40
- */
41
- export const httpStatusOf = (error: unknown): number | null => {
42
- if (error == null || typeof error !== 'object') {
43
- return null
44
- }
45
- const stamped = httpStatus((error as ResponseStatusCarrier).responseStatus)
46
- if (stamped != null) {
47
- return stamped
48
- }
49
- if (error instanceof ApiClientError && httpStatus(error.status) != null) {
50
- return error.status!
51
- }
52
- const declaration = error.constructor as { httpStatus?: unknown, allowServerErrorStatus?: unknown } | undefined
53
- const declared = httpStatus(declaration?.httpStatus)
54
- if (declared != null && (declared < 500 || declaration?.allowServerErrorStatus === true)) {
55
- return declared
56
- }
57
-
58
- return markerOf(error)?.status ?? null
59
- }
60
-
61
- /** The server's incident id carried by an error — its own field, else its marker — or `null`. */
62
- export const incidentIdOf = (error: unknown): string | null => {
63
- if (error == null || typeof error !== 'object') {
64
- return null
65
- }
66
- const own = (error as { incidentId?: unknown }).incidentId
67
- if (typeof own === 'string' && own !== '') {
68
- return own
69
- }
70
-
71
- return markerOf(error)?.incidentId ?? null
72
- }
73
-
74
- /**
75
- * Whether a response body is a production incident body — the bare incident UUID an
76
- * `@owlmeans/server-api` boundary sends instead of the marshaled error. A development body
77
- * carries the `ResilientError` separator and is never one.
78
- */
79
- export const isIncidentBody = (data: unknown): data is string =>
80
- typeof data === 'string' && !data.includes(ResilientError.separator) && UUID.test(data.trim())
81
-
82
- export type { ResponseStatusCarrier } from './types.js'
10
+ /** @deprecated compat:factory-refactor — use `apiStatusHelper.httpStatusOf(…)` */
11
+ export const httpStatusOf = (error: unknown): number | null => apiStatusHelper.httpStatusOf(error)
@@ -9,3 +9,28 @@ export interface ClientMarker {
9
9
  status?: number
10
10
  incidentId?: string
11
11
  }
12
+
13
+ /** Reads the HTTP status, the incident id and the denial kind a failed call carries. */
14
+ export interface ApiStatusHelper {
15
+ /** True only for a server-labelled auth/IAM refusal, never an unrelated 403. */
16
+ isAccessDenied: (error: unknown) => boolean
17
+ /**
18
+ * The HTTP status an error answers to, or `null` when nothing states one.
19
+ *
20
+ * Read in order: the status the API client stamped on the error it rebuilt from a response
21
+ * (`responseStatus`); the status an `ApiClientError` parsed from its marker; the `httpStatus` its
22
+ * class declares — a 4xx always, a 5xx only with `allowServerErrorStatus`, exactly what an
23
+ * `@owlmeans/server-api` boundary answers with; finally an `api:client:*` marker in the message or
24
+ * the type of anything else. This is what lets a browser recognise a refusal from its status
25
+ * alone when a production body carries nothing but an incident id.
26
+ */
27
+ httpStatusOf: (error: unknown) => number | null
28
+ /** The server's incident id carried by an error — its own field, else its marker — or `null`. */
29
+ incidentIdOf: (error: unknown) => string | null
30
+ /**
31
+ * Whether a response body is a production incident body — the bare incident UUID an
32
+ * `@owlmeans/server-api` boundary sends instead of the marshaled error. A development body
33
+ * carries the `ResilientError` separator and is never one.
34
+ */
35
+ isIncidentBody: (data: unknown) => data is string
36
+ }
@@ -0,0 +1,37 @@
1
+ import type { RouteMethod } from '@owlmeans/route'
2
+
3
+ export interface RequestBody {
4
+ /** What axios is handed as `data`. */
5
+ data: unknown
6
+ /** A `content-type` the request lacked and must carry. */
7
+ contentType?: string
8
+ /** `data` is final text — axios must not transform it again. */
9
+ verbatim: boolean
10
+ }
11
+
12
+ /** How a request's body travels: its content type and the text a scalar body becomes. */
13
+ export interface BodyUtils {
14
+ /** The request's own `content-type`, whatever case its key is written in. */
15
+ contentTypeOf: (headers?: Record<string, unknown>) => string | undefined
16
+ /** `application/json` or a `+json` suffix type, with or without parameters. */
17
+ isJsonContentType: (type?: string) => boolean
18
+ /**
19
+ * The JSON text a scalar body travels as, so the server parses it back to the same value.
20
+ *
21
+ * A number or a boolean is `JSON.stringify`'d. A string is a value unless it is already JSON text:
22
+ * one that does not parse is quoted; one that parses is sent as it is (the caller serialized it
23
+ * itself) — except under a body schema of `type: 'string'`, where only a JSON string literal
24
+ * counts as serialized and `123`, `true` or `{…}` are the text the caller means.
25
+ */
26
+ jsonScalarBody: (body: string | number | boolean, schema?: object) => string
27
+ /**
28
+ * What a request's body becomes on the wire.
29
+ *
30
+ * - A `x-www-form-urlencoded` request: the body through `qs`.
31
+ * - A string, number or boolean under a JSON `content-type` — the caller's, or `application/json`
32
+ * supplied for a `POST` that names none — becomes JSON text ({@link BodyUtils.jsonScalarBody}).
33
+ * - Anything else — objects, arrays, a scalar under another content type — goes to axios as it is,
34
+ * which serializes an object or an array as JSON.
35
+ */
36
+ requestBodyOf: (body: unknown, headers: Record<string, unknown> | undefined, method?: RouteMethod, schema?: object) => RequestBody
37
+ }