hanzio 2.1.0 → 2.2.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 +21 -0
- package/README.md +5 -6
- package/dist/api-wrapper/endpoint.d.ts +8 -5
- package/dist/api-wrapper/transport.d.ts +1 -0
- package/dist/api-wrapper/types.d.ts +7 -0
- package/dist/{index-j1ybktbr.js → index-zbj9s3s3.js} +6 -4
- package/dist/src/api-wrapper/index.js +1 -1
- package/dist/src/secrets/index.js +1 -1
- package/package.json +10 -3
- package/src/api-wrapper/README.md +738 -0
- package/src/api-wrapper/create-client.ts +9 -3
- package/src/api-wrapper/endpoint.ts +19 -5
- package/src/api-wrapper/transport.ts +6 -2
- package/src/api-wrapper/types.ts +13 -0
- package/src/crypto/README.md +144 -0
- package/src/format/README.md +120 -0
- package/src/i18n/README.md +65 -0
- package/src/jwt/README.md +70 -0
- package/src/p-queue/README.md +237 -0
- package/src/secrets/README.md +413 -0
- package/src/sitemap/README.md +61 -0
- package/src/storage/README.md +71 -0
- package/src/typedswitch/README.md +319 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kristoffer Hansen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1156,9 +1156,8 @@ bun run build
|
|
|
1156
1156
|
|
|
1157
1157
|
## Releasing
|
|
1158
1158
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
publishes to npm. Nobody runs `npm publish` locally.
|
|
1159
|
+
Squash PRs with conventional titles (`fix:`, `feat:`, or `feat!:`). Release Please keeps the version and changelog in a release PR; merge that PR to publish with release notes and npm provenance. See [release and recovery instructions](docs/releasing.md).
|
|
1160
|
+
|
|
1161
|
+
## License
|
|
1162
|
+
|
|
1163
|
+
[MIT](LICENSE).
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
|
-
import type { ApiEndpoint } from './types';
|
|
2
|
+
import type { ApiEndpoint, CheckResponseContext } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Defines an endpoint with full inference, as an alternative to
|
|
5
5
|
* `satisfies ApiEndpoint`: `resFormatter` must return what `resSchema`
|
|
6
|
-
* accepts,
|
|
7
|
-
*
|
|
6
|
+
* accepts, `checkResponse` receives the parsed `resSchema` output, and a
|
|
7
|
+
* literal `path` types its `:params` (making `reqParams` required when
|
|
8
|
+
* there's no `reqParamsSchema`).
|
|
8
9
|
*
|
|
9
10
|
* @example
|
|
10
11
|
* export const usersGet = defineEndpoint({
|
|
@@ -13,6 +14,8 @@ import type { ApiEndpoint } from './types';
|
|
|
13
14
|
* resSchema: User
|
|
14
15
|
* })
|
|
15
16
|
*/
|
|
16
|
-
export declare function defineEndpoint<const TEndpoint extends ApiEndpoint>(endpoint: TEndpoint & {
|
|
17
|
-
|
|
17
|
+
export declare function defineEndpoint<const TEndpoint extends Omit<ApiEndpoint, 'checkResponse'>, TRes extends z.ZodType = TEndpoint['resSchema']>(endpoint: TEndpoint & {
|
|
18
|
+
resSchema: TRes;
|
|
19
|
+
resFormatter?: (data: unknown, headers: Record<string, string>) => z.input<TRes>;
|
|
20
|
+
checkResponse?: ((data: z.output<TRes>, context: CheckResponseContext) => void | Promise<void>) | false;
|
|
18
21
|
}): TEndpoint;
|
|
@@ -49,6 +49,7 @@ export declare function makeRequestWithRetry<TValidated>(args: MakeRequestArgs<T
|
|
|
49
49
|
export declare function validateAndTransformResponse<T extends z.ZodType>(endpoint: ApiEndpoint<z.ZodType | undefined, z.ZodType | undefined, z.ZodType | undefined, z.ZodType | undefined, T>, response: Response, ctx: (over?: Partial<ApiErrorContext>) => ApiErrorContext): Promise<{
|
|
50
50
|
data: z.infer<T>;
|
|
51
51
|
responseSizeBytes: number;
|
|
52
|
+
headers: Record<string, string>;
|
|
52
53
|
}>;
|
|
53
54
|
export declare function defaultShouldRetry(ctx: RetryContext): boolean;
|
|
54
55
|
export {};
|
|
@@ -17,6 +17,11 @@ export interface ApiEndpoint<TReqBody extends z.ZodType | undefined = z.ZodType
|
|
|
17
17
|
resFormatter?: (data: unknown, headers: Record<string, string>) => z.input<TResponse>;
|
|
18
18
|
reqDefaultQueryParams?: QueryParams;
|
|
19
19
|
doNotEncodeQueryParams?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Replaces the client's `checkResponse` for this endpoint; `false` skips it.
|
|
22
|
+
* `defineEndpoint` types `data` as the parsed `resSchema` output.
|
|
23
|
+
*/
|
|
24
|
+
checkResponse?: ((data: any, context: CheckResponseContext) => void | Promise<void>) | false;
|
|
20
25
|
}
|
|
21
26
|
export interface ApiWrapperResponse<T> {
|
|
22
27
|
data: T;
|
|
@@ -25,6 +30,8 @@ export interface ApiWrapperResponse<T> {
|
|
|
25
30
|
responseTimeMs: number;
|
|
26
31
|
httpStatus: number;
|
|
27
32
|
retryCount: number;
|
|
33
|
+
/** Response headers of the final attempt, keyed by lowercase name. */
|
|
34
|
+
headers: Record<string, string>;
|
|
28
35
|
}
|
|
29
36
|
export type RetryContext = {
|
|
30
37
|
error?: unknown;
|
|
@@ -1006,7 +1006,7 @@ async function validateAndTransformResponse(endpoint, response, ctx) {
|
|
|
1006
1006
|
}
|
|
1007
1007
|
const transformedData = endpoint.resFormatter ? endpoint.resFormatter(responseData, responseHeaders) : responseData;
|
|
1008
1008
|
const data = await validateResponse(endpoint.resSchema, transformedData, ctx);
|
|
1009
|
-
return { data, responseSizeBytes };
|
|
1009
|
+
return { data, responseSizeBytes, headers: responseHeaders };
|
|
1010
1010
|
}
|
|
1011
1011
|
function defaultShouldRetry2(ctx) {
|
|
1012
1012
|
if (ctx.error)
|
|
@@ -1170,14 +1170,16 @@ function createApiClientImpl(apiConfig) {
|
|
|
1170
1170
|
responseSizeMb: result.validatedData.responseSizeBytes / (1024 * 1024),
|
|
1171
1171
|
responseTimeMs: elapsed(),
|
|
1172
1172
|
httpStatus: result.httpStatus,
|
|
1173
|
-
retryCount: result.retryCount
|
|
1173
|
+
retryCount: result.retryCount,
|
|
1174
|
+
headers: result.validatedData.headers
|
|
1174
1175
|
};
|
|
1175
1176
|
};
|
|
1176
1177
|
const runCheckResponse = async (data, httpStatus, attempt) => {
|
|
1177
|
-
|
|
1178
|
+
const checkResponse = endpoint.checkResponse === undefined ? apiConfig.checkResponse : endpoint.checkResponse;
|
|
1179
|
+
if (!checkResponse)
|
|
1178
1180
|
return;
|
|
1179
1181
|
try {
|
|
1180
|
-
await
|
|
1182
|
+
await checkResponse(data, {
|
|
1181
1183
|
endpoint: endpointName,
|
|
1182
1184
|
method: endpoint.method,
|
|
1183
1185
|
url: fullUrl,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hanzio",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A lightweight set of utility functions for TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/src/index.js",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"src/**/*.ts",
|
|
28
|
+
"src/**/README.md",
|
|
28
29
|
"dist",
|
|
29
30
|
"!src/**/*.test.ts"
|
|
30
31
|
],
|
|
@@ -132,7 +133,8 @@
|
|
|
132
133
|
"typecheck": "tsgo --noEmit",
|
|
133
134
|
"prepare": "husky",
|
|
134
135
|
"test:secrets:runtime": "bun run build && node scripts/verify-secrets.mjs",
|
|
135
|
-
"test:exports": "bun run build && node scripts/verify-exports.mjs"
|
|
136
|
+
"test:exports": "bun run build && node scripts/verify-exports.mjs",
|
|
137
|
+
"verify:package": "node scripts/verify-package.mjs"
|
|
136
138
|
},
|
|
137
139
|
"devDependencies": {
|
|
138
140
|
"@biomejs/biome": "^2.4.6",
|
|
@@ -152,5 +154,10 @@
|
|
|
152
154
|
"zod": {
|
|
153
155
|
"optional": true
|
|
154
156
|
}
|
|
155
|
-
}
|
|
157
|
+
},
|
|
158
|
+
"homepage": "https://github.com/HansKristoffer/hanzio#readme",
|
|
159
|
+
"bugs": {
|
|
160
|
+
"url": "https://github.com/HansKristoffer/hanzio/issues"
|
|
161
|
+
},
|
|
162
|
+
"packageManager": "bun@1.4.2"
|
|
156
163
|
}
|