@zelkovajs/adapter-http 0.0.0-stage → 0.6.0
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/dist/adapter.d.ts +77 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +356 -0
- package/dist/adapter.js.map +1 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +55 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/path.d.ts +43 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +90 -0
- package/dist/path.js.map +1 -0
- package/package.json +43 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { AdapterCallContext, AdapterOperation, ConfiguredAdapter, DataAdapter, Duration } from '@zelkovajs/core/contracts';
|
|
3
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
4
|
+
/** A value of a fixed query field. */
|
|
5
|
+
export type HttpQueryConstant = string | number | boolean;
|
|
6
|
+
/**
|
|
7
|
+
* One named, reviewed request to the API.
|
|
8
|
+
*
|
|
9
|
+
* Each key of `input` goes to one place: the path, the query or the JSON body. A
|
|
10
|
+
* `:name` in `path` sends the field `name` in the path. `query` and `body` list the
|
|
11
|
+
* other fields.
|
|
12
|
+
*/
|
|
13
|
+
export interface HttpOperation {
|
|
14
|
+
method: HttpMethod;
|
|
15
|
+
/** A path template below `baseUrl`, for example `/customers/:id`. */
|
|
16
|
+
path: string;
|
|
17
|
+
/** A `z.object` schema. */
|
|
18
|
+
input: z.ZodTypeAny;
|
|
19
|
+
/** If it is given, the adapter checks the parsed JSON body of the reply against it. */
|
|
20
|
+
output?: z.ZodTypeAny;
|
|
21
|
+
effect: 'read' | 'write';
|
|
22
|
+
/** The input fields that go to the query. */
|
|
23
|
+
query?: readonly string[];
|
|
24
|
+
/** The input fields that go to the JSON body. A `GET` operation has no body. */
|
|
25
|
+
body?: readonly string[];
|
|
26
|
+
/**
|
|
27
|
+
* Query fields with a fixed value, for example a project ID. The adapter adds them
|
|
28
|
+
* to each request, and a caller cannot change them. An input field with the same
|
|
29
|
+
* name is a startup error.
|
|
30
|
+
*/
|
|
31
|
+
fixedQuery?: Readonly<Record<string, HttpQueryConstant>>;
|
|
32
|
+
}
|
|
33
|
+
export type HttpOperations = Record<string, HttpOperation>;
|
|
34
|
+
/** The configuration of SPECS.md, section "Shipped adapters", part "HTTP API". */
|
|
35
|
+
export interface HttpConfig {
|
|
36
|
+
/** The fixed base URL, for example `https://api.example.com/v1`. Each operation path goes below it. */
|
|
37
|
+
baseUrl: string;
|
|
38
|
+
/**
|
|
39
|
+
* The name of the stored secret `connection.<credentials>`. The secret holds a bearer
|
|
40
|
+
* token, or JSON `{ "header": "...", "value": "..." }` for a different header.
|
|
41
|
+
* Without `credentials`, the adapter sends no credential.
|
|
42
|
+
*/
|
|
43
|
+
credentials?: string;
|
|
44
|
+
/** The longest time of one request, with the reply body. Default `30s`. */
|
|
45
|
+
timeout?: Duration;
|
|
46
|
+
operations: HttpOperations;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A database adapter that sends named, reviewed requests to one REST API.
|
|
50
|
+
*
|
|
51
|
+
* The base URL is fixed. The adapter URL-encodes each path value, so an operation
|
|
52
|
+
* cannot reach a different host or a path outside the base URL. The constructor checks
|
|
53
|
+
* each operation, and it refuses to start on a problem.
|
|
54
|
+
*/
|
|
55
|
+
export declare class HttpApiAdapter implements DataAdapter {
|
|
56
|
+
#private;
|
|
57
|
+
/** Added by `@DatabaseAdapter`. Returns the adapter for the `adapters` of the application module. */
|
|
58
|
+
static configure: (config: HttpConfig) => ConfiguredAdapter;
|
|
59
|
+
constructor(config: HttpConfig);
|
|
60
|
+
operations(): Readonly<Record<string, AdapterOperation>>;
|
|
61
|
+
/**
|
|
62
|
+
* Reads the credential. It sends no request, because an API has no common request
|
|
63
|
+
* that checks a credential. The API rejects a wrong credential at the first call.
|
|
64
|
+
* @throws {ZelkovaError} with the code `source_unavailable` and the reason
|
|
65
|
+
* `credential_invalid` if the credential is empty, or if its JSON form is not valid.
|
|
66
|
+
*/
|
|
67
|
+
open(credential: string | null): Promise<void>;
|
|
68
|
+
close(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Sends the request of one listed operation, and returns the parsed JSON body of the
|
|
71
|
+
* reply. An empty body is `null`.
|
|
72
|
+
* @throws {ZelkovaError} with the code `internal` for an operation that the adapter
|
|
73
|
+
* does not list. The adapter then sends no request.
|
|
74
|
+
*/
|
|
75
|
+
execute(operation: string, input: unknown, ctx: AdapterCallContext): Promise<unknown>;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAMhI,MAAM,MAAM,UAA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AC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;;;;OAKG;IACG,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,OAAO,CAAC;CA4G5F"}
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { DatabaseAdapter, ZelkovaError, isZodSchema, parseDuration } from '@zelkovajs/core/contracts';
|
|
12
|
+
import { toHttpError } from './errors.js';
|
|
13
|
+
import { compilePath, expandPath } from './path.js';
|
|
14
|
+
const METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']);
|
|
15
|
+
/** A header name: a token of RFC 9110. */
|
|
16
|
+
const HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
17
|
+
/** A header value: visible characters, spaces and tabs only. */
|
|
18
|
+
const HEADER_VALUE = /^[\t\x20-\x7e\x80-\xff]+$/;
|
|
19
|
+
const configSchema = z.object({
|
|
20
|
+
baseUrl: z.string().min(1),
|
|
21
|
+
credentials: z.string().optional(),
|
|
22
|
+
timeout: z.string().optional(),
|
|
23
|
+
operations: z.record(z.string(), z.unknown()),
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* A database adapter that sends named, reviewed requests to one REST API.
|
|
27
|
+
*
|
|
28
|
+
* The base URL is fixed. The adapter URL-encodes each path value, so an operation
|
|
29
|
+
* cannot reach a different host or a path outside the base URL. The constructor checks
|
|
30
|
+
* each operation, and it refuses to start on a problem.
|
|
31
|
+
*/
|
|
32
|
+
let HttpApiAdapter = class HttpApiAdapter {
|
|
33
|
+
#base;
|
|
34
|
+
#timeoutMs;
|
|
35
|
+
#compiled;
|
|
36
|
+
#operations;
|
|
37
|
+
/** `undefined` before `open`. `null` after `open` without a credential. */
|
|
38
|
+
#auth = undefined;
|
|
39
|
+
constructor(config) {
|
|
40
|
+
const problems = [];
|
|
41
|
+
const parsed = configSchema.safeParse(config);
|
|
42
|
+
if (!parsed.success) {
|
|
43
|
+
problems.push(...parsed.error.issues.map((issue) => `The setting "${issue.path.join('.')}" is not valid: ${issue.message}`));
|
|
44
|
+
}
|
|
45
|
+
const base = parseBaseUrl(config?.baseUrl, problems);
|
|
46
|
+
let timeoutMs = 30_000;
|
|
47
|
+
if (config?.timeout !== undefined) {
|
|
48
|
+
try {
|
|
49
|
+
timeoutMs = parseDuration(config.timeout);
|
|
50
|
+
if (timeoutMs < 1)
|
|
51
|
+
problems.push('The setting "timeout" must be longer than 0ms.');
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
problems.push(error.message);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const compiled = new Map();
|
|
58
|
+
for (const [name, operation] of Object.entries(config?.operations ?? {})) {
|
|
59
|
+
const result = compileOperation(name, operation);
|
|
60
|
+
if ('problems' in result)
|
|
61
|
+
problems.push(...result.problems);
|
|
62
|
+
else
|
|
63
|
+
compiled.set(name, result);
|
|
64
|
+
}
|
|
65
|
+
if (problems.length > 0 || base === null) {
|
|
66
|
+
throw new ZelkovaError('invalid_input', `The HTTP API adapter has problems:\n- ${problems.join('\n- ')}`, { problems });
|
|
67
|
+
}
|
|
68
|
+
this.#base = base;
|
|
69
|
+
this.#timeoutMs = timeoutMs;
|
|
70
|
+
this.#compiled = compiled;
|
|
71
|
+
this.#operations = Object.freeze(Object.fromEntries([...compiled].map(([name, op]) => [name, op.spec])));
|
|
72
|
+
}
|
|
73
|
+
operations() {
|
|
74
|
+
return this.#operations;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Reads the credential. It sends no request, because an API has no common request
|
|
78
|
+
* that checks a credential. The API rejects a wrong credential at the first call.
|
|
79
|
+
* @throws {ZelkovaError} with the code `source_unavailable` and the reason
|
|
80
|
+
* `credential_invalid` if the credential is empty, or if its JSON form is not valid.
|
|
81
|
+
*/
|
|
82
|
+
async open(credential) {
|
|
83
|
+
this.#auth = parseCredential(credential);
|
|
84
|
+
}
|
|
85
|
+
async close() {
|
|
86
|
+
this.#auth = undefined;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Sends the request of one listed operation, and returns the parsed JSON body of the
|
|
90
|
+
* reply. An empty body is `null`.
|
|
91
|
+
* @throws {ZelkovaError} with the code `internal` for an operation that the adapter
|
|
92
|
+
* does not list. The adapter then sends no request.
|
|
93
|
+
*/
|
|
94
|
+
async execute(operation, input, ctx) {
|
|
95
|
+
const compiled = this.#compiled.get(operation);
|
|
96
|
+
if (compiled === undefined) {
|
|
97
|
+
throw new ZelkovaError('internal', `The HTTP API adapter does not list the operation "${operation}".`, { operation });
|
|
98
|
+
}
|
|
99
|
+
const parsed = compiled.spec.input.safeParse(input);
|
|
100
|
+
if (!parsed.success) {
|
|
101
|
+
throw new ZelkovaError('invalid_input', `The input of the HTTP operation "${operation}" is not valid:\n${z.prettifyError(parsed.error)}`, {
|
|
102
|
+
operation,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const auth = this.#auth;
|
|
106
|
+
if (auth === undefined)
|
|
107
|
+
throw new ZelkovaError('internal', 'The HTTP API adapter is not open.', { operation });
|
|
108
|
+
if (ctx.signal.aborted)
|
|
109
|
+
throw abortError(ctx.signal, operation);
|
|
110
|
+
const data = (parsed.data ?? {});
|
|
111
|
+
const url = this.#url(operation, compiled, data);
|
|
112
|
+
const headers = new Headers({ accept: 'application/json' });
|
|
113
|
+
if (auth !== null)
|
|
114
|
+
headers.set(auth.name, auth.value);
|
|
115
|
+
if (compiled.spec.effect === 'write')
|
|
116
|
+
headers.set('idempotency-key', ctx.idempotencyKey);
|
|
117
|
+
let body;
|
|
118
|
+
if (compiled.body !== null) {
|
|
119
|
+
body = JSON.stringify(Object.fromEntries(compiled.body.filter((field) => data[field] !== undefined).map((field) => [field, data[field]])));
|
|
120
|
+
headers.set('content-type', 'application/json');
|
|
121
|
+
}
|
|
122
|
+
const { status, replyHeaders, text } = await this.#send(operation, url, { method: compiled.method, headers, body }, ctx.signal);
|
|
123
|
+
if (status < 200 || status >= 300)
|
|
124
|
+
throw toHttpError(operation, status, replyHeaders);
|
|
125
|
+
let result = null;
|
|
126
|
+
if (text.trim() !== '') {
|
|
127
|
+
try {
|
|
128
|
+
result = JSON.parse(text);
|
|
129
|
+
}
|
|
130
|
+
catch (cause) {
|
|
131
|
+
throw new ZelkovaError('internal', `The HTTP operation "${operation}" got a reply that is not JSON.`, { operation, status, reason: 'bad_reply' }, { cause });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (compiled.spec.output !== undefined && !compiled.spec.output.safeParse(result).success) {
|
|
135
|
+
throw new ZelkovaError('internal', `The reply of the HTTP operation "${operation}" does not match its output schema.`, {
|
|
136
|
+
operation,
|
|
137
|
+
status,
|
|
138
|
+
reason: 'output_mismatch',
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
/** Makes the URL of one call. The URL stays below the base URL. */
|
|
144
|
+
#url(operation, compiled, data) {
|
|
145
|
+
const path = expandPath(compiled.path, data);
|
|
146
|
+
if (!path.ok) {
|
|
147
|
+
throw new ZelkovaError('invalid_input', `The path field "${path.field}" of the HTTP operation "${operation}" has no valid value.`, {
|
|
148
|
+
operation,
|
|
149
|
+
field: path.field,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const basePath = this.#base.pathname.replace(/\/+$/, '');
|
|
153
|
+
const url = new URL(`${this.#base.origin}${basePath}${path.path}`);
|
|
154
|
+
// The checks of the path template and of each value keep the URL below the base URL. This check is a second guard.
|
|
155
|
+
if (url.origin !== this.#base.origin || !url.pathname.startsWith(`${basePath}/`)) {
|
|
156
|
+
throw new ZelkovaError('internal', `The HTTP operation "${operation}" made a URL outside the base URL.`, { operation });
|
|
157
|
+
}
|
|
158
|
+
for (const [name, value] of compiled.fixedQuery)
|
|
159
|
+
url.searchParams.append(name, value);
|
|
160
|
+
for (const field of compiled.query) {
|
|
161
|
+
const values = queryValues(data[field]);
|
|
162
|
+
if (values === null) {
|
|
163
|
+
throw new ZelkovaError('invalid_input', `The query field "${field}" of the HTTP operation "${operation}" is not a string, a number, a boolean or a list of them.`, {
|
|
164
|
+
operation,
|
|
165
|
+
field,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
for (const value of values)
|
|
169
|
+
url.searchParams.append(field, value);
|
|
170
|
+
}
|
|
171
|
+
return url;
|
|
172
|
+
}
|
|
173
|
+
/** Sends one request, and reads the reply body. An abort or the timeout stops the request and the read. */
|
|
174
|
+
async #send(operation, url, init, signal) {
|
|
175
|
+
const controller = new AbortController();
|
|
176
|
+
let timedOut = false;
|
|
177
|
+
const timer = setTimeout(() => {
|
|
178
|
+
timedOut = true;
|
|
179
|
+
controller.abort();
|
|
180
|
+
}, this.#timeoutMs);
|
|
181
|
+
const onAbort = () => controller.abort();
|
|
182
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
183
|
+
try {
|
|
184
|
+
// The base URL is fixed, so the adapter does not follow a redirect to a different URL.
|
|
185
|
+
const response = await fetch(url, { ...init, signal: controller.signal, redirect: 'manual' });
|
|
186
|
+
const text = await response.text();
|
|
187
|
+
return { status: response.status, replyHeaders: response.headers, text };
|
|
188
|
+
}
|
|
189
|
+
catch (cause) {
|
|
190
|
+
if (signal.aborted)
|
|
191
|
+
throw abortError(signal, operation);
|
|
192
|
+
if (timedOut) {
|
|
193
|
+
throw new ZelkovaError('timeout', `The HTTP operation "${operation}" did not end in ${this.#timeoutMs}ms.`, { operation }, { cause });
|
|
194
|
+
}
|
|
195
|
+
// The cause can name the host, but not the path, the query or a header.
|
|
196
|
+
throw new ZelkovaError('source_unavailable', `The HTTP operation "${operation}" cannot reach the API.`, { operation, reason: 'unreachable' }, { cause });
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
clearTimeout(timer);
|
|
200
|
+
signal.removeEventListener('abort', onAbort);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
HttpApiAdapter = __decorate([
|
|
205
|
+
DatabaseAdapter({ kind: 'http' }),
|
|
206
|
+
__metadata("design:paramtypes", [Object])
|
|
207
|
+
], HttpApiAdapter);
|
|
208
|
+
export { HttpApiAdapter };
|
|
209
|
+
/** The error of an aborted call: the reason of the signal if it is a Zelkova error, else `cancelled`. */
|
|
210
|
+
function abortError(signal, operation) {
|
|
211
|
+
const reason = signal.reason;
|
|
212
|
+
if (reason instanceof ZelkovaError)
|
|
213
|
+
return reason;
|
|
214
|
+
return new ZelkovaError('cancelled', `The HTTP operation "${operation}" stopped because its signal aborted.`, { operation });
|
|
215
|
+
}
|
|
216
|
+
function parseBaseUrl(value, problems) {
|
|
217
|
+
if (typeof value !== 'string' || value === '')
|
|
218
|
+
return null;
|
|
219
|
+
let url;
|
|
220
|
+
try {
|
|
221
|
+
url = new URL(value);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
problems.push(`The setting "baseUrl" is not a URL: "${value}".`);
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
const before = problems.length;
|
|
228
|
+
if (url.protocol !== 'https:' && url.protocol !== 'http:')
|
|
229
|
+
problems.push('The setting "baseUrl" must use https or http.');
|
|
230
|
+
if (url.username !== '' || url.password !== '')
|
|
231
|
+
problems.push('The setting "baseUrl" must not hold a user or a password. Use "credentials".');
|
|
232
|
+
if (url.search !== '' || url.hash !== '')
|
|
233
|
+
problems.push('The setting "baseUrl" must not hold a query or a fragment. Use "fixedQuery".');
|
|
234
|
+
return problems.length === before ? url : null;
|
|
235
|
+
}
|
|
236
|
+
/** Reads the stored credential: a bearer token, or JSON `{ header, value }`. */
|
|
237
|
+
function parseCredential(credential) {
|
|
238
|
+
if (credential === null)
|
|
239
|
+
return null;
|
|
240
|
+
const invalid = (text) => new ZelkovaError('source_unavailable', `The credential of the HTTP API adapter is not valid: ${text}`, { reason: 'credential_invalid' });
|
|
241
|
+
const trimmed = credential.trim();
|
|
242
|
+
if (trimmed === '')
|
|
243
|
+
throw invalid('it is empty.');
|
|
244
|
+
if (trimmed.startsWith('{')) {
|
|
245
|
+
let value;
|
|
246
|
+
try {
|
|
247
|
+
value = JSON.parse(trimmed);
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
throw invalid('it starts with "{", and it is not JSON.');
|
|
251
|
+
}
|
|
252
|
+
const { header, value: secret } = (value ?? {});
|
|
253
|
+
if (typeof header !== 'string' || !HEADER_NAME.test(header))
|
|
254
|
+
throw invalid('the JSON form needs a "header" with a valid header name.');
|
|
255
|
+
if (typeof secret !== 'string' || !HEADER_VALUE.test(secret))
|
|
256
|
+
throw invalid('the JSON form needs a "value" with a valid header value.');
|
|
257
|
+
return { name: header, value: secret };
|
|
258
|
+
}
|
|
259
|
+
if (!HEADER_VALUE.test(trimmed))
|
|
260
|
+
throw invalid('the token holds a character that a header cannot hold.');
|
|
261
|
+
return { name: 'authorization', value: `Bearer ${trimmed}` };
|
|
262
|
+
}
|
|
263
|
+
/** The text values of one query field, or `null` for a value that a query cannot hold. `undefined` and `null` send nothing. */
|
|
264
|
+
function queryValues(value) {
|
|
265
|
+
if (value === undefined || value === null)
|
|
266
|
+
return [];
|
|
267
|
+
const one = (item) => {
|
|
268
|
+
if (typeof item === 'string' || typeof item === 'number' || typeof item === 'bigint' || typeof item === 'boolean')
|
|
269
|
+
return String(item);
|
|
270
|
+
if (item instanceof Date)
|
|
271
|
+
return item.toISOString();
|
|
272
|
+
return null;
|
|
273
|
+
};
|
|
274
|
+
const items = Array.isArray(value) ? value : [value];
|
|
275
|
+
const texts = items.map(one);
|
|
276
|
+
return texts.every((text) => text !== null) ? texts : null;
|
|
277
|
+
}
|
|
278
|
+
function compileOperation(name, operation) {
|
|
279
|
+
const op = operation;
|
|
280
|
+
const problems = [];
|
|
281
|
+
const label = `The operation "${name}"`;
|
|
282
|
+
if (typeof op?.method !== 'string' || !METHODS.has(op.method))
|
|
283
|
+
problems.push(`${label} has no method: use ${[...METHODS].join(', ')}.`);
|
|
284
|
+
if (typeof op?.path !== 'string')
|
|
285
|
+
problems.push(`${label} has no path.`);
|
|
286
|
+
if (!isZodSchema(op?.input))
|
|
287
|
+
problems.push(`${label} has no input schema.`);
|
|
288
|
+
if (op?.output !== undefined && !isZodSchema(op.output))
|
|
289
|
+
problems.push(`${label} has an output that is not a schema.`);
|
|
290
|
+
if (op?.effect !== 'read' && op?.effect !== 'write')
|
|
291
|
+
problems.push(`${label} has no effect: use 'read' or 'write'.`);
|
|
292
|
+
const lists = { query: op?.query, body: op?.body };
|
|
293
|
+
for (const [key, list] of Object.entries(lists)) {
|
|
294
|
+
if (list !== undefined && (!Array.isArray(list) || !list.every((item) => typeof item === 'string')))
|
|
295
|
+
problems.push(`${label} has a "${key}" that is not a list of field names.`);
|
|
296
|
+
}
|
|
297
|
+
const fixed = op?.fixedQuery;
|
|
298
|
+
const fixedOk = fixed === undefined ||
|
|
299
|
+
(typeof fixed === 'object' && fixed !== null && Object.values(fixed).every((value) => ['string', 'number', 'boolean'].includes(typeof value)));
|
|
300
|
+
if (!fixedOk)
|
|
301
|
+
problems.push(`${label} has a "fixedQuery" that is not an object of strings, numbers and booleans.`);
|
|
302
|
+
if (problems.length > 0)
|
|
303
|
+
return { problems };
|
|
304
|
+
const spec = op;
|
|
305
|
+
const path = compilePath(spec.path);
|
|
306
|
+
if (!path.ok)
|
|
307
|
+
return { problems: path.problems.map((problem) => `${label}: ${problem}`) };
|
|
308
|
+
const shape = objectShape(spec.input);
|
|
309
|
+
if (shape === null)
|
|
310
|
+
return { problems: [`${label} has an input schema that is not a z.object.`] };
|
|
311
|
+
const query = spec.query ?? [];
|
|
312
|
+
const body = spec.body ?? null;
|
|
313
|
+
if (body !== null && spec.method === 'GET')
|
|
314
|
+
problems.push(`${label} is a GET request, and a GET request has no body.`);
|
|
315
|
+
const fields = Object.keys(shape);
|
|
316
|
+
const places = new Map();
|
|
317
|
+
const place = (field, where) => {
|
|
318
|
+
places.set(field, [...(places.get(field) ?? []), where]);
|
|
319
|
+
};
|
|
320
|
+
path.path.fields.forEach((field) => place(field, 'the path'));
|
|
321
|
+
query.forEach((field) => place(field, 'the query'));
|
|
322
|
+
(body ?? []).forEach((field) => place(field, 'the body'));
|
|
323
|
+
for (const [field, where] of places) {
|
|
324
|
+
if (!fields.includes(field))
|
|
325
|
+
problems.push(`${label} sends the field "${field}" in ${where.join(' and ')}, and the input schema has no such key.`);
|
|
326
|
+
else if (where.length > 1)
|
|
327
|
+
problems.push(`${label} sends the field "${field}" in ${where.join(' and ')}. Send each field in one place.`);
|
|
328
|
+
}
|
|
329
|
+
for (const field of fields) {
|
|
330
|
+
if (!places.has(field))
|
|
331
|
+
problems.push(`${label} does not send the input field "${field}". Put it in the path, "query" or "body".`);
|
|
332
|
+
}
|
|
333
|
+
const fixedQuery = Object.entries(spec.fixedQuery ?? {}).map(([key, value]) => [key, String(value)]);
|
|
334
|
+
for (const [key] of fixedQuery) {
|
|
335
|
+
if (fields.includes(key))
|
|
336
|
+
problems.push(`${label} has the fixed query field "${key}", and its input schema has a field with the same name.`);
|
|
337
|
+
}
|
|
338
|
+
if (problems.length > 0)
|
|
339
|
+
return { problems };
|
|
340
|
+
return {
|
|
341
|
+
spec: { input: spec.input, ...(spec.output === undefined ? {} : { output: spec.output }), effect: spec.effect },
|
|
342
|
+
method: spec.method,
|
|
343
|
+
path: path.path,
|
|
344
|
+
query,
|
|
345
|
+
body,
|
|
346
|
+
fixedQuery,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/** The keys of a `z.object` schema, or `null` for each other schema. */
|
|
350
|
+
function objectShape(schema) {
|
|
351
|
+
const def = schema._zod?.def;
|
|
352
|
+
if (def?.type !== 'object')
|
|
353
|
+
return null;
|
|
354
|
+
return schema.shape;
|
|
355
|
+
}
|
|
356
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAGtG,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAoEpD,MAAM,OAAO,GAAwB,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;AAExF,0CAA0C;AAC1C,MAAM,WAAW,GAAG,+BAA+B,CAAC;AAEpD,gEAAgE;AAChE,MAAM,YAAY,GAAG,2BAA2B,CAAC;AAEjD,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IAC5B,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC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C,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAU,CAAC,CAAC;IAC9G,KAAK,MAAM,CAAC,GAAG,CAAC,IAAI,UAAU,EAAE,CAAC;QAC/B,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC;YAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,KAAK,+BAA+B,GAAG,yDAAyD,CAAC,CAAC;IAC/I,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;IAE7C,OAAO;QACL,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE;QAC/G,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,KAAK;QACL,IAAI;QACJ,UAAU;KACX,CAAC;AACJ,CAAC;AAED,wEAAwE;AACxE,SAAS,WAAW,CAAC,MAAoB;IACvC,MAAM,GAAG,GAAI,MAAkD,CAAC,IAAI,EAAE,GAAG,CAAC;IAC1E,IAAI,GAAG,EAAE,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACxC,OAAQ,MAAiC,CAAC,KAAK,CAAC;AAClD,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ZelkovaError } from '@zelkovajs/core/contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Turns a reply status that is not 2xx into a contract error, with the rules of
|
|
4
|
+
* SPECS.md, section "Shipped adapters", part "HTTP API".
|
|
5
|
+
*
|
|
6
|
+
* - `401` and `403` are `source_unavailable` with `details.reason = 'credential_rejected'`.
|
|
7
|
+
* - `404` is `not_found`, and `409` is `conflict`.
|
|
8
|
+
* - `429` is `rate_limited`, with `details.retryAfter` if the API sends `Retry-After`.
|
|
9
|
+
* - `408` is `timeout`.
|
|
10
|
+
* - `5xx` is `source_unavailable`, with `details.retryAfter` if the API sends `Retry-After`.
|
|
11
|
+
* - `400` and `422` are `invalid_input`.
|
|
12
|
+
* - A redirect is `internal`. The adapter does not follow a redirect, because the base URL is fixed.
|
|
13
|
+
* - Each other status is `internal`.
|
|
14
|
+
*
|
|
15
|
+
* The message and the details hold the operation name and the status only. They hold
|
|
16
|
+
* no URL, no header and no body, so no token and no query value goes to a log.
|
|
17
|
+
*/
|
|
18
|
+
export declare function toHttpError(operation: string, status: number, headers: Headers): ZelkovaError;
|
|
19
|
+
/** The whole seconds of a `Retry-After` header, or `undefined`. The header holds seconds or an HTTP date. */
|
|
20
|
+
export declare function retryAfterSeconds(raw: string | null, now?: number): number | undefined;
|
|
21
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,YAAY,CAsB7F;AAED,6GAA6G;AAC7G,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,GAAE,MAAmB,GAAG,MAAM,GAAG,SAAS,CAMlG"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ZelkovaError } from '@zelkovajs/core/contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Turns a reply status that is not 2xx into a contract error, with the rules of
|
|
4
|
+
* SPECS.md, section "Shipped adapters", part "HTTP API".
|
|
5
|
+
*
|
|
6
|
+
* - `401` and `403` are `source_unavailable` with `details.reason = 'credential_rejected'`.
|
|
7
|
+
* - `404` is `not_found`, and `409` is `conflict`.
|
|
8
|
+
* - `429` is `rate_limited`, with `details.retryAfter` if the API sends `Retry-After`.
|
|
9
|
+
* - `408` is `timeout`.
|
|
10
|
+
* - `5xx` is `source_unavailable`, with `details.retryAfter` if the API sends `Retry-After`.
|
|
11
|
+
* - `400` and `422` are `invalid_input`.
|
|
12
|
+
* - A redirect is `internal`. The adapter does not follow a redirect, because the base URL is fixed.
|
|
13
|
+
* - Each other status is `internal`.
|
|
14
|
+
*
|
|
15
|
+
* The message and the details hold the operation name and the status only. They hold
|
|
16
|
+
* no URL, no header and no body, so no token and no query value goes to a log.
|
|
17
|
+
*/
|
|
18
|
+
export function toHttpError(operation, status, headers) {
|
|
19
|
+
const make = (code, message, extra = {}) => new ZelkovaError(code, message, { operation, status, ...extra });
|
|
20
|
+
const where = `The HTTP operation "${operation}"`;
|
|
21
|
+
const retryAfter = retryAfterSeconds(headers.get('retry-after'));
|
|
22
|
+
const retry = retryAfter === undefined ? {} : { retryAfter: `${retryAfter}s` };
|
|
23
|
+
if (status === 401 || status === 403) {
|
|
24
|
+
return make('source_unavailable', `${where} failed with status ${status}: the API rejected the credential.`, { reason: 'credential_rejected' });
|
|
25
|
+
}
|
|
26
|
+
if (status === 404)
|
|
27
|
+
return make('not_found', `${where} failed with status 404: the API did not find the record.`);
|
|
28
|
+
if (status === 409)
|
|
29
|
+
return make('conflict', `${where} failed with status 409: the API reported a conflict.`);
|
|
30
|
+
if (status === 429)
|
|
31
|
+
return make('rate_limited', `${where} failed with status 429: the API limited the rate of calls.`, retry);
|
|
32
|
+
if (status === 408)
|
|
33
|
+
return make('timeout', `${where} failed with status 408: the API did not answer in time.`);
|
|
34
|
+
if (status >= 500)
|
|
35
|
+
return make('source_unavailable', `${where} failed with status ${status}.`, { reason: 'server_error', ...retry });
|
|
36
|
+
if (status === 400 || status === 422)
|
|
37
|
+
return make('invalid_input', `${where} failed with status ${status}: the API refused the request.`);
|
|
38
|
+
if (status >= 300 && status < 400) {
|
|
39
|
+
return make('internal', `${where} failed with status ${status}: the API sent a redirect, and the adapter does not follow one.`, {
|
|
40
|
+
reason: 'redirect_refused',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return make('internal', `${where} failed with status ${status}.`);
|
|
44
|
+
}
|
|
45
|
+
/** The whole seconds of a `Retry-After` header, or `undefined`. The header holds seconds or an HTTP date. */
|
|
46
|
+
export function retryAfterSeconds(raw, now = Date.now()) {
|
|
47
|
+
if (raw === null || raw.trim() === '')
|
|
48
|
+
return undefined;
|
|
49
|
+
const seconds = Number(raw);
|
|
50
|
+
if (Number.isFinite(seconds))
|
|
51
|
+
return seconds >= 0 ? Math.ceil(seconds) : undefined;
|
|
52
|
+
const date = Date.parse(raw);
|
|
53
|
+
return Number.isNaN(date) ? undefined : Math.max(0, Math.ceil((date - now) / 1000));
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,WAAW,CAAC,SAAiB,EAAE,MAAc,EAAE,OAAgB;IAC7E,MAAM,IAAI,GAAG,CAAC,IAA0B,EAAE,OAAe,EAAE,QAAiC,EAAE,EAAgB,EAAE,CAC9G,IAAI,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;IACnE,MAAM,KAAK,GAAG,uBAAuB,SAAS,GAAG,CAAC;IAClD,MAAM,UAAU,GAAG,iBAAiB,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;IACjE,MAAM,KAAK,GAAG,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,OAAO,IAAI,CAAC,UAAU,EAAE,GAAG,KAAK,uBAAuB,MAAM,GAAG,CAAC,CAAC;AACpE,CAAC;AAED,6GAA6G;AAC7G,MAAM,UAAU,iBAAiB,CAAC,GAAkB,EAAE,MAAc,IAAI,CAAC,GAAG,EAAE;IAC5E,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,SAAS,CAAC;IACxD,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5B,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACnF,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC7B,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;AACtF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { HttpApiAdapter } from './adapter.js';
|
|
2
|
+
export type { HttpConfig, HttpMethod, HttpOperation, HttpOperations, HttpQueryConstant } from './adapter.js';
|
|
3
|
+
export { retryAfterSeconds, toHttpError } from './errors.js';
|
|
4
|
+
export { compilePath, expandPath } from './path.js';
|
|
5
|
+
export type { CompilePathResult, CompiledPath, PathSegment } from './path.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7G,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACpD,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/path.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** One segment of a path template: a fixed text, or an input field with a fixed suffix. */
|
|
2
|
+
export type PathSegment = {
|
|
3
|
+
literal: string;
|
|
4
|
+
} | {
|
|
5
|
+
field: string;
|
|
6
|
+
suffix: string;
|
|
7
|
+
};
|
|
8
|
+
/** A path template after the check at startup. */
|
|
9
|
+
export interface CompiledPath {
|
|
10
|
+
segments: readonly PathSegment[];
|
|
11
|
+
/** The input fields of the path, in order. */
|
|
12
|
+
fields: readonly string[];
|
|
13
|
+
}
|
|
14
|
+
export type CompilePathResult = {
|
|
15
|
+
ok: true;
|
|
16
|
+
path: CompiledPath;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
problems: string[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Checks a path template such as `/customers/:id`, and finds its input fields.
|
|
23
|
+
*
|
|
24
|
+
* The template starts with `/`. It holds no query, no fragment, no backslash and no
|
|
25
|
+
* `.` or `..` segment. Only a `:name` at the start of a segment is a field, so
|
|
26
|
+
* `/v1/items:search` holds no field.
|
|
27
|
+
*/
|
|
28
|
+
export declare function compilePath(template: string): CompilePathResult;
|
|
29
|
+
/** True for a segment that a URL parser reads as `.` or `..`, also in a percent-encoded form. */
|
|
30
|
+
export declare function isDotSegment(segment: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Puts the input values into the path. Each value is URL-encoded, so a value cannot
|
|
33
|
+
* add a segment, a query or a host.
|
|
34
|
+
* @returns the path, or the name of the field that has no valid value.
|
|
35
|
+
*/
|
|
36
|
+
export declare function expandPath(path: CompiledPath, values: Readonly<Record<string, unknown>>): {
|
|
37
|
+
ok: true;
|
|
38
|
+
path: string;
|
|
39
|
+
} | {
|
|
40
|
+
ok: false;
|
|
41
|
+
field: string;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=path.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,MAAM,MAAM,WAAW,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAElF,kDAAkD;AAClD,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,SAAS,WAAW,EAAE,CAAC;IACjC,8CAA8C;IAC9C,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,MAAM,iBAAiB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AASrG;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,iBAAiB,CAqC/D;AAED,iGAAiG;AACjG,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAQrD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAkBnJ"}
|
package/dist/path.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** A path field: `:` and a name at the start of a segment, for example `:id` in `/customers/:id`. */
|
|
2
|
+
const FIELD = /^:([A-Za-z_][A-Za-z0-9_]*)(.*)$/;
|
|
3
|
+
/** Characters that a fixed part of a path must not hold. */
|
|
4
|
+
// eslint-disable-next-line no-control-regex -- the check refuses control characters.
|
|
5
|
+
const FORBIDDEN = /[?#\\\s\u0000-\u001f\u007f]/;
|
|
6
|
+
/**
|
|
7
|
+
* Checks a path template such as `/customers/:id`, and finds its input fields.
|
|
8
|
+
*
|
|
9
|
+
* The template starts with `/`. It holds no query, no fragment, no backslash and no
|
|
10
|
+
* `.` or `..` segment. Only a `:name` at the start of a segment is a field, so
|
|
11
|
+
* `/v1/items:search` holds no field.
|
|
12
|
+
*/
|
|
13
|
+
export function compilePath(template) {
|
|
14
|
+
if (!template.startsWith('/'))
|
|
15
|
+
return { ok: false, problems: [`The path "${template}" does not start with "/".`] };
|
|
16
|
+
const problems = [];
|
|
17
|
+
const segments = [];
|
|
18
|
+
const fields = [];
|
|
19
|
+
const parts = template.slice(1).split('/');
|
|
20
|
+
parts.forEach((part, index) => {
|
|
21
|
+
// A path can end with "/". Each other segment has a value.
|
|
22
|
+
if (part === '' && index === parts.length - 1) {
|
|
23
|
+
segments.push({ literal: '' });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (part === '') {
|
|
27
|
+
problems.push(`The path "${template}" has an empty segment.`);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const match = FIELD.exec(part);
|
|
31
|
+
const literal = match === null ? part : match[2];
|
|
32
|
+
if (FORBIDDEN.test(literal)) {
|
|
33
|
+
problems.push(`The path "${template}" holds a query, a fragment, a backslash or a space. Use the query fields for a query.`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (match === null && isDotSegment(part)) {
|
|
37
|
+
problems.push(`The path "${template}" has a "." or ".." segment.`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (match === null) {
|
|
41
|
+
segments.push({ literal: part });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const field = match[1];
|
|
45
|
+
if (fields.includes(field))
|
|
46
|
+
problems.push(`The path "${template}" names the field "${field}" two times.`);
|
|
47
|
+
fields.push(field);
|
|
48
|
+
segments.push({ field, suffix: literal });
|
|
49
|
+
});
|
|
50
|
+
if (problems.length > 0)
|
|
51
|
+
return { ok: false, problems };
|
|
52
|
+
return { ok: true, path: { segments, fields } };
|
|
53
|
+
}
|
|
54
|
+
/** True for a segment that a URL parser reads as `.` or `..`, also in a percent-encoded form. */
|
|
55
|
+
export function isDotSegment(segment) {
|
|
56
|
+
let decoded;
|
|
57
|
+
try {
|
|
58
|
+
decoded = decodeURIComponent(segment);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return decoded === '.' || decoded === '..';
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Puts the input values into the path. Each value is URL-encoded, so a value cannot
|
|
67
|
+
* add a segment, a query or a host.
|
|
68
|
+
* @returns the path, or the name of the field that has no valid value.
|
|
69
|
+
*/
|
|
70
|
+
export function expandPath(path, values) {
|
|
71
|
+
let result = '';
|
|
72
|
+
for (const segment of path.segments) {
|
|
73
|
+
if ('literal' in segment) {
|
|
74
|
+
result += `/${segment.literal}`;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const value = values[segment.field];
|
|
78
|
+
if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'bigint' && typeof value !== 'boolean') {
|
|
79
|
+
return { ok: false, field: segment.field };
|
|
80
|
+
}
|
|
81
|
+
const text = String(value);
|
|
82
|
+
const encoded = `${encodeURIComponent(text)}${segment.suffix}`;
|
|
83
|
+
// An empty value removes the segment, and a dot segment moves the path up.
|
|
84
|
+
if (text === '' || isDotSegment(encoded))
|
|
85
|
+
return { ok: false, field: segment.field };
|
|
86
|
+
result += `/${encoded}`;
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, path: result };
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=path.js.map
|
package/dist/path.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.js","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AAYA,qGAAqG;AACrG,MAAM,KAAK,GAAG,iCAAiC,CAAC;AAEhD,4DAA4D;AAC5D,qFAAqF;AACrF,MAAM,SAAS,GAAG,6BAA6B,CAAC;AAEhD;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,QAAgB;IAC1C,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,aAAa,QAAQ,4BAA4B,CAAC,EAAE,CAAC;IACnH,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,QAAQ,GAAkB,EAAE,CAAC;IACnC,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3C,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;QAC5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,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC/D,2EAA2E;QAC3E,IAAI,IAAI,KAAK,EAAE,IAAI,YAAY,CAAC,OAAO,CAAC;YAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;QACrF,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;IAC1B,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AACpC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zelkovajs/adapter-http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "The HTTP API database adapter of Zelkova: named, reviewed requests to a REST API.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/KontextMachine/zelkova.git",
|
|
9
|
+
"directory": "packages/adapter-http"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/KontextMachine/zelkova/tree/main/packages/adapter-http#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/KontextMachine/zelkova/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"NOTICE"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"zod": "4.6.7"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@zelkovajs/core": "^0.6.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@zelkovajs/core": "0.6.0"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=24"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.build.json",
|
|
44
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
45
|
+
}
|
|
6
46
|
}
|
package/README.md
DELETED