@deliverart/sdk-js-core 0.2.0 → 1.1.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/package.json +8 -25
- package/.changeset/config.json +0 -11
- package/.github/workflows/workflow.yml +0 -47
- package/.prettierrc +0 -7
- package/CHANGELOG.md +0 -73
- package/README.md +0 -3
- package/dist/index.cjs +0 -128
- package/dist/index.d.cts +0 -54
- package/dist/index.d.ts +0 -54
- package/dist/index.js +0 -88
- package/eslint.config.js +0 -37
- package/src/ApiClient.ts +0 -132
- package/src/errors.ts +0 -17
- package/src/index.ts +0 -3
- package/src/types.ts +0 -16
- package/tsconfig.json +0 -15
package/package.json
CHANGED
|
@@ -1,44 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deliverart/sdk-js-core",
|
|
3
3
|
"description": "Core SDK for DeliverArt, providing essential functionalities and utilities.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"tsup": {
|
|
9
|
+
"tsconfig": "./tsconfig.json"
|
|
10
|
+
},
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
10
13
|
"import": "./dist/index.js",
|
|
11
14
|
"require": "./dist/index.cjs"
|
|
12
15
|
}
|
|
13
16
|
},
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
},
|
|
18
|
-
"devDependencies": {
|
|
19
|
-
"@changesets/cli": "^2.29.4",
|
|
20
|
-
"@eslint/js": "9.28.0",
|
|
21
|
-
"@types/node": "22.15.30",
|
|
22
|
-
"@typescript-eslint/eslint-plugin": "8.33.1",
|
|
23
|
-
"@typescript-eslint/parser": "8.33.1",
|
|
24
|
-
"eslint": "9.28.0",
|
|
25
|
-
"eslint-config-prettier": "10.1.5",
|
|
26
|
-
"eslint-plugin-simple-import-sort": "12.1.1",
|
|
27
|
-
"prettier": "3.5.3",
|
|
28
|
-
"tsup": "8.5.0",
|
|
29
|
-
"typescript": "5.8.3"
|
|
30
|
-
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
31
20
|
"publishConfig": {
|
|
32
21
|
"access": "public"
|
|
33
22
|
},
|
|
34
23
|
"scripts": {
|
|
35
24
|
"build": "tsup src/index.ts --dts --format esm,cjs",
|
|
36
|
-
"dev": "tsup src/index.ts --dts --watch"
|
|
37
|
-
"lint": "eslint .",
|
|
38
|
-
"lint:fix": "eslint . --fix",
|
|
39
|
-
"prettier": "prettier --check .",
|
|
40
|
-
"prettier:fix": "prettier --write .",
|
|
41
|
-
"version": "@changesets/cli version",
|
|
42
|
-
"release": "@changesets/cli publish"
|
|
25
|
+
"dev": "tsup src/index.ts --dts --watch"
|
|
43
26
|
}
|
|
44
27
|
}
|
package/.changeset/config.json
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json",
|
|
3
|
-
"changelog": "@changesets/cli/changelog",
|
|
4
|
-
"commit": false,
|
|
5
|
-
"fixed": [],
|
|
6
|
-
"linked": [],
|
|
7
|
-
"access": "restricted",
|
|
8
|
-
"baseBranch": "main",
|
|
9
|
-
"updateInternalDependencies": "patch",
|
|
10
|
-
"ignore": []
|
|
11
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
name: Release
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- main
|
|
7
|
-
|
|
8
|
-
permissions:
|
|
9
|
-
contents: write
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
release:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v4
|
|
16
|
-
|
|
17
|
-
- uses: pnpm/action-setup@v2
|
|
18
|
-
with:
|
|
19
|
-
version: 8
|
|
20
|
-
|
|
21
|
-
- uses: actions/setup-node@v4
|
|
22
|
-
with:
|
|
23
|
-
node-version: '20'
|
|
24
|
-
registry-url: 'https://registry.npmjs.org'
|
|
25
|
-
always-auth: true
|
|
26
|
-
env:
|
|
27
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
28
|
-
|
|
29
|
-
- name: Install deps
|
|
30
|
-
run: pnpm install
|
|
31
|
-
|
|
32
|
-
- name: Build
|
|
33
|
-
run: pnpm build
|
|
34
|
-
|
|
35
|
-
- name: Create version and changelog
|
|
36
|
-
run: pnpx @changesets/cli version
|
|
37
|
-
|
|
38
|
-
- name: Publish to NPM
|
|
39
|
-
run: pnpx @changesets/cli publish
|
|
40
|
-
env:
|
|
41
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
42
|
-
|
|
43
|
-
- name: Push updated versions and changelogs
|
|
44
|
-
uses: EndBug/add-and-commit@v9
|
|
45
|
-
with:
|
|
46
|
-
message: 'chore(release): version bump'
|
|
47
|
-
add: '.'
|
package/.prettierrc
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# @deliverart/sdk-js-core
|
|
2
|
-
|
|
3
|
-
## 0.2.0
|
|
4
|
-
|
|
5
|
-
### Minor Changes
|
|
6
|
-
|
|
7
|
-
- 3a95890: change input output mode
|
|
8
|
-
|
|
9
|
-
## 0.1.4
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- 60fba1c: ignore lock
|
|
14
|
-
|
|
15
|
-
## 0.1.3
|
|
16
|
-
|
|
17
|
-
### Patch Changes
|
|
18
|
-
|
|
19
|
-
- e177edc: Expose rawResponse
|
|
20
|
-
|
|
21
|
-
## 0.1.2
|
|
22
|
-
|
|
23
|
-
### Patch Changes
|
|
24
|
-
|
|
25
|
-
- b204aab: Add parseResponse method
|
|
26
|
-
|
|
27
|
-
## 0.1.1
|
|
28
|
-
|
|
29
|
-
### Patch Changes
|
|
30
|
-
|
|
31
|
-
- 20b57df: Expose errors
|
|
32
|
-
|
|
33
|
-
## 0.1.0
|
|
34
|
-
|
|
35
|
-
### Minor Changes
|
|
36
|
-
|
|
37
|
-
- dd33eb4: Implement Request Response call
|
|
38
|
-
|
|
39
|
-
## 0.0.7
|
|
40
|
-
|
|
41
|
-
### Patch Changes
|
|
42
|
-
|
|
43
|
-
- c36723b: Extends ApiExtension with void type
|
|
44
|
-
|
|
45
|
-
## 0.0.6
|
|
46
|
-
|
|
47
|
-
### Patch Changes
|
|
48
|
-
|
|
49
|
-
- 14cc1fd: restructure ApiClient to use factory function and improve type definitions
|
|
50
|
-
|
|
51
|
-
## 0.0.5
|
|
52
|
-
|
|
53
|
-
### Patch Changes
|
|
54
|
-
|
|
55
|
-
- 077e5f6: make use() generically typed with proper plugin propagation
|
|
56
|
-
|
|
57
|
-
## 0.0.4
|
|
58
|
-
|
|
59
|
-
### Patch Changes
|
|
60
|
-
|
|
61
|
-
- b5f002a: fix: update package name
|
|
62
|
-
|
|
63
|
-
## 0.0.3
|
|
64
|
-
|
|
65
|
-
### Patch Changes
|
|
66
|
-
|
|
67
|
-
- e7475f6: fix: remove changeset default files
|
|
68
|
-
|
|
69
|
-
## 0.0.2
|
|
70
|
-
|
|
71
|
-
### Patch Changes
|
|
72
|
-
|
|
73
|
-
- 0f3205e: fix: trigger deploy
|
package/README.md
DELETED
package/dist/index.cjs
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
AbstractApiRequest: () => AbstractApiRequest,
|
|
34
|
-
InputValidationError: () => InputValidationError,
|
|
35
|
-
OutputValidationError: () => OutputValidationError,
|
|
36
|
-
createApiClient: () => createApiClient
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(index_exports);
|
|
39
|
-
|
|
40
|
-
// src/ApiClient.ts
|
|
41
|
-
var import_axios = __toESM(require("axios"), 1);
|
|
42
|
-
|
|
43
|
-
// src/errors.ts
|
|
44
|
-
var InputValidationError = class extends Error {
|
|
45
|
-
// eslint-disable-next-line no-unused-vars
|
|
46
|
-
constructor(issues) {
|
|
47
|
-
super("Invalid input");
|
|
48
|
-
this.issues = issues;
|
|
49
|
-
this.name = "InputValidationError";
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
var OutputValidationError = class extends Error {
|
|
53
|
-
// eslint-disable-next-line no-unused-vars
|
|
54
|
-
constructor(issues) {
|
|
55
|
-
super("Invalid response");
|
|
56
|
-
this.issues = issues;
|
|
57
|
-
this.name = "OutputValidationError";
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// src/ApiClient.ts
|
|
62
|
-
var AbstractApiRequest = class {
|
|
63
|
-
constructor(input, options) {
|
|
64
|
-
this.input = input;
|
|
65
|
-
this.options = options;
|
|
66
|
-
}
|
|
67
|
-
validateInput() {
|
|
68
|
-
const result = this.inputSchema.safeParse(this.input);
|
|
69
|
-
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
70
|
-
return result.data;
|
|
71
|
-
}
|
|
72
|
-
validateQuery() {
|
|
73
|
-
if (!this.querySchema || !this.options?.query) return void 0;
|
|
74
|
-
const result = this.querySchema.safeParse(this.options.query);
|
|
75
|
-
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
76
|
-
return result.data;
|
|
77
|
-
}
|
|
78
|
-
validateHeaders() {
|
|
79
|
-
if (!this.headersSchema || !this.options?.headers) return void 0;
|
|
80
|
-
const result = this.headersSchema.safeParse(this.options.headers);
|
|
81
|
-
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
82
|
-
return result.data;
|
|
83
|
-
}
|
|
84
|
-
validateOutput(data) {
|
|
85
|
-
const result = this.outputSchema.safeParse(data);
|
|
86
|
-
if (!result.success) throw new OutputValidationError(result.error.issues);
|
|
87
|
-
return result.data;
|
|
88
|
-
}
|
|
89
|
-
parseResponse(data, rawResponse) {
|
|
90
|
-
void rawResponse;
|
|
91
|
-
return this.validateOutput(data);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
function createApiClient(config) {
|
|
95
|
-
const http = import_axios.default.create({ baseURL: config.baseUrl });
|
|
96
|
-
const base = {
|
|
97
|
-
http,
|
|
98
|
-
async call(request) {
|
|
99
|
-
const input = request.validateInput();
|
|
100
|
-
const query = request.validateQuery();
|
|
101
|
-
const headers = request.validateHeaders();
|
|
102
|
-
const res = await http.request({
|
|
103
|
-
url: request.getPath(),
|
|
104
|
-
method: request.method,
|
|
105
|
-
headers: {
|
|
106
|
-
"Content-Type": request.contentType,
|
|
107
|
-
Accept: request.accept,
|
|
108
|
-
...headers ?? {}
|
|
109
|
-
},
|
|
110
|
-
params: query,
|
|
111
|
-
data: request.method !== "GET" ? input : void 0
|
|
112
|
-
});
|
|
113
|
-
return request.parseResponse(res.data, res);
|
|
114
|
-
},
|
|
115
|
-
addPlugin(plugin) {
|
|
116
|
-
const extension = plugin.setup(this);
|
|
117
|
-
return { ...this, ...extension };
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
return base;
|
|
121
|
-
}
|
|
122
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
123
|
-
0 && (module.exports = {
|
|
124
|
-
AbstractApiRequest,
|
|
125
|
-
InputValidationError,
|
|
126
|
-
OutputValidationError,
|
|
127
|
-
createApiClient
|
|
128
|
-
});
|
package/dist/index.d.cts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { AxiosResponse, AxiosInstance } from 'axios';
|
|
2
|
-
import { ZodType, ZodTypeDef, ZodSchema, ZodIssue } from 'zod';
|
|
3
|
-
|
|
4
|
-
type ApiExtension = Record<string, unknown>;
|
|
5
|
-
interface ApiClientPlugin<T extends ApiExtension = Record<string, unknown>> {
|
|
6
|
-
setup: (client: ApiClient<Record<string, unknown>>) => T;
|
|
7
|
-
}
|
|
8
|
-
type ZodInput<T extends ZodType<any, any, any>> = T extends ZodType<any, any, infer I> ? I : never;
|
|
9
|
-
type ZodOutput<T extends ZodType<any, any, any>> = T extends ZodType<infer O, any, any> ? O : never;
|
|
10
|
-
|
|
11
|
-
declare abstract class AbstractApiRequest<TInputSchema extends ZodType<any, ZodTypeDef, any>, TOutputSchema extends ZodType<any, ZodTypeDef, any>, Query = unknown, Headers = unknown> {
|
|
12
|
-
readonly input: ZodInput<TInputSchema>;
|
|
13
|
-
readonly options?: {
|
|
14
|
-
query?: Query;
|
|
15
|
-
headers?: Headers;
|
|
16
|
-
} | undefined;
|
|
17
|
-
abstract readonly method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
18
|
-
abstract readonly contentType: 'application/json' | 'multipart/form-data' | 'application/merge-patch+json';
|
|
19
|
-
abstract readonly accept: 'application/json';
|
|
20
|
-
abstract readonly inputSchema: TInputSchema;
|
|
21
|
-
abstract readonly outputSchema: TOutputSchema;
|
|
22
|
-
abstract readonly querySchema?: ZodSchema<Query>;
|
|
23
|
-
abstract readonly headersSchema?: ZodSchema<Headers>;
|
|
24
|
-
protected constructor(input: ZodInput<TInputSchema>, options?: {
|
|
25
|
-
query?: Query;
|
|
26
|
-
headers?: Headers;
|
|
27
|
-
} | undefined);
|
|
28
|
-
abstract getPath(): string;
|
|
29
|
-
validateInput(): ZodOutput<TInputSchema>;
|
|
30
|
-
validateQuery(): Query | undefined;
|
|
31
|
-
validateHeaders(): Headers | undefined;
|
|
32
|
-
validateOutput(data: unknown): ZodOutput<TOutputSchema>;
|
|
33
|
-
parseResponse(data: unknown, rawResponse?: AxiosResponse): ZodOutput<TOutputSchema>;
|
|
34
|
-
}
|
|
35
|
-
interface ApiClientBase {
|
|
36
|
-
http: AxiosInstance;
|
|
37
|
-
call<TInputSchema extends ZodType<any, ZodTypeDef, any>, TOutputSchema extends ZodType<any, ZodTypeDef, any>, Q = unknown, H = unknown>(request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>): Promise<ZodOutput<TOutputSchema>>;
|
|
38
|
-
addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>;
|
|
39
|
-
}
|
|
40
|
-
type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
|
|
41
|
-
declare function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
|
|
42
|
-
baseUrl: string;
|
|
43
|
-
}): ApiClient<Extensions>;
|
|
44
|
-
|
|
45
|
-
declare class InputValidationError extends Error {
|
|
46
|
-
readonly issues: ZodIssue[];
|
|
47
|
-
constructor(issues: ZodIssue[]);
|
|
48
|
-
}
|
|
49
|
-
declare class OutputValidationError extends Error {
|
|
50
|
-
readonly issues: ZodIssue[];
|
|
51
|
-
constructor(issues: ZodIssue[]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, InputValidationError, OutputValidationError, type ZodInput, type ZodOutput, createApiClient };
|
package/dist/index.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { AxiosResponse, AxiosInstance } from 'axios';
|
|
2
|
-
import { ZodType, ZodTypeDef, ZodSchema, ZodIssue } from 'zod';
|
|
3
|
-
|
|
4
|
-
type ApiExtension = Record<string, unknown>;
|
|
5
|
-
interface ApiClientPlugin<T extends ApiExtension = Record<string, unknown>> {
|
|
6
|
-
setup: (client: ApiClient<Record<string, unknown>>) => T;
|
|
7
|
-
}
|
|
8
|
-
type ZodInput<T extends ZodType<any, any, any>> = T extends ZodType<any, any, infer I> ? I : never;
|
|
9
|
-
type ZodOutput<T extends ZodType<any, any, any>> = T extends ZodType<infer O, any, any> ? O : never;
|
|
10
|
-
|
|
11
|
-
declare abstract class AbstractApiRequest<TInputSchema extends ZodType<any, ZodTypeDef, any>, TOutputSchema extends ZodType<any, ZodTypeDef, any>, Query = unknown, Headers = unknown> {
|
|
12
|
-
readonly input: ZodInput<TInputSchema>;
|
|
13
|
-
readonly options?: {
|
|
14
|
-
query?: Query;
|
|
15
|
-
headers?: Headers;
|
|
16
|
-
} | undefined;
|
|
17
|
-
abstract readonly method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
18
|
-
abstract readonly contentType: 'application/json' | 'multipart/form-data' | 'application/merge-patch+json';
|
|
19
|
-
abstract readonly accept: 'application/json';
|
|
20
|
-
abstract readonly inputSchema: TInputSchema;
|
|
21
|
-
abstract readonly outputSchema: TOutputSchema;
|
|
22
|
-
abstract readonly querySchema?: ZodSchema<Query>;
|
|
23
|
-
abstract readonly headersSchema?: ZodSchema<Headers>;
|
|
24
|
-
protected constructor(input: ZodInput<TInputSchema>, options?: {
|
|
25
|
-
query?: Query;
|
|
26
|
-
headers?: Headers;
|
|
27
|
-
} | undefined);
|
|
28
|
-
abstract getPath(): string;
|
|
29
|
-
validateInput(): ZodOutput<TInputSchema>;
|
|
30
|
-
validateQuery(): Query | undefined;
|
|
31
|
-
validateHeaders(): Headers | undefined;
|
|
32
|
-
validateOutput(data: unknown): ZodOutput<TOutputSchema>;
|
|
33
|
-
parseResponse(data: unknown, rawResponse?: AxiosResponse): ZodOutput<TOutputSchema>;
|
|
34
|
-
}
|
|
35
|
-
interface ApiClientBase {
|
|
36
|
-
http: AxiosInstance;
|
|
37
|
-
call<TInputSchema extends ZodType<any, ZodTypeDef, any>, TOutputSchema extends ZodType<any, ZodTypeDef, any>, Q = unknown, H = unknown>(request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>): Promise<ZodOutput<TOutputSchema>>;
|
|
38
|
-
addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>;
|
|
39
|
-
}
|
|
40
|
-
type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
|
|
41
|
-
declare function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
|
|
42
|
-
baseUrl: string;
|
|
43
|
-
}): ApiClient<Extensions>;
|
|
44
|
-
|
|
45
|
-
declare class InputValidationError extends Error {
|
|
46
|
-
readonly issues: ZodIssue[];
|
|
47
|
-
constructor(issues: ZodIssue[]);
|
|
48
|
-
}
|
|
49
|
-
declare class OutputValidationError extends Error {
|
|
50
|
-
readonly issues: ZodIssue[];
|
|
51
|
-
constructor(issues: ZodIssue[]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, InputValidationError, OutputValidationError, type ZodInput, type ZodOutput, createApiClient };
|
package/dist/index.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
// src/ApiClient.ts
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
|
|
4
|
-
// src/errors.ts
|
|
5
|
-
var InputValidationError = class extends Error {
|
|
6
|
-
// eslint-disable-next-line no-unused-vars
|
|
7
|
-
constructor(issues) {
|
|
8
|
-
super("Invalid input");
|
|
9
|
-
this.issues = issues;
|
|
10
|
-
this.name = "InputValidationError";
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
var OutputValidationError = class extends Error {
|
|
14
|
-
// eslint-disable-next-line no-unused-vars
|
|
15
|
-
constructor(issues) {
|
|
16
|
-
super("Invalid response");
|
|
17
|
-
this.issues = issues;
|
|
18
|
-
this.name = "OutputValidationError";
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// src/ApiClient.ts
|
|
23
|
-
var AbstractApiRequest = class {
|
|
24
|
-
constructor(input, options) {
|
|
25
|
-
this.input = input;
|
|
26
|
-
this.options = options;
|
|
27
|
-
}
|
|
28
|
-
validateInput() {
|
|
29
|
-
const result = this.inputSchema.safeParse(this.input);
|
|
30
|
-
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
31
|
-
return result.data;
|
|
32
|
-
}
|
|
33
|
-
validateQuery() {
|
|
34
|
-
if (!this.querySchema || !this.options?.query) return void 0;
|
|
35
|
-
const result = this.querySchema.safeParse(this.options.query);
|
|
36
|
-
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
37
|
-
return result.data;
|
|
38
|
-
}
|
|
39
|
-
validateHeaders() {
|
|
40
|
-
if (!this.headersSchema || !this.options?.headers) return void 0;
|
|
41
|
-
const result = this.headersSchema.safeParse(this.options.headers);
|
|
42
|
-
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
43
|
-
return result.data;
|
|
44
|
-
}
|
|
45
|
-
validateOutput(data) {
|
|
46
|
-
const result = this.outputSchema.safeParse(data);
|
|
47
|
-
if (!result.success) throw new OutputValidationError(result.error.issues);
|
|
48
|
-
return result.data;
|
|
49
|
-
}
|
|
50
|
-
parseResponse(data, rawResponse) {
|
|
51
|
-
void rawResponse;
|
|
52
|
-
return this.validateOutput(data);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
function createApiClient(config) {
|
|
56
|
-
const http = axios.create({ baseURL: config.baseUrl });
|
|
57
|
-
const base = {
|
|
58
|
-
http,
|
|
59
|
-
async call(request) {
|
|
60
|
-
const input = request.validateInput();
|
|
61
|
-
const query = request.validateQuery();
|
|
62
|
-
const headers = request.validateHeaders();
|
|
63
|
-
const res = await http.request({
|
|
64
|
-
url: request.getPath(),
|
|
65
|
-
method: request.method,
|
|
66
|
-
headers: {
|
|
67
|
-
"Content-Type": request.contentType,
|
|
68
|
-
Accept: request.accept,
|
|
69
|
-
...headers ?? {}
|
|
70
|
-
},
|
|
71
|
-
params: query,
|
|
72
|
-
data: request.method !== "GET" ? input : void 0
|
|
73
|
-
});
|
|
74
|
-
return request.parseResponse(res.data, res);
|
|
75
|
-
},
|
|
76
|
-
addPlugin(plugin) {
|
|
77
|
-
const extension = plugin.setup(this);
|
|
78
|
-
return { ...this, ...extension };
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
return base;
|
|
82
|
-
}
|
|
83
|
-
export {
|
|
84
|
-
AbstractApiRequest,
|
|
85
|
-
InputValidationError,
|
|
86
|
-
OutputValidationError,
|
|
87
|
-
createApiClient
|
|
88
|
-
};
|
package/eslint.config.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
// eslint.config.js per @typescript-eslint v8
|
|
3
|
-
import js from '@eslint/js'
|
|
4
|
-
import prettier from 'eslint-config-prettier'
|
|
5
|
-
import simpleImportSortPlugin from 'eslint-plugin-simple-import-sort'
|
|
6
|
-
import tsPlugin from '@typescript-eslint/eslint-plugin'
|
|
7
|
-
import tsParser from '@typescript-eslint/parser'
|
|
8
|
-
|
|
9
|
-
export default [
|
|
10
|
-
js.configs.recommended,
|
|
11
|
-
{
|
|
12
|
-
files: ['**/*.ts'],
|
|
13
|
-
languageOptions: {
|
|
14
|
-
parser: tsParser,
|
|
15
|
-
globals: {
|
|
16
|
-
process: 'readonly',
|
|
17
|
-
},
|
|
18
|
-
parserOptions: {
|
|
19
|
-
project: ['./tsconfig.json'],
|
|
20
|
-
tsconfigRootDir: process.cwd(),
|
|
21
|
-
sourceType: 'module',
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
plugins: {
|
|
25
|
-
'@typescript-eslint': tsPlugin,
|
|
26
|
-
'simple-import-sort': simpleImportSortPlugin,
|
|
27
|
-
},
|
|
28
|
-
rules: {
|
|
29
|
-
'simple-import-sort/imports': 'error',
|
|
30
|
-
'simple-import-sort/exports': 'error',
|
|
31
|
-
'@typescript-eslint/no-unused-vars': 'warn',
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
prettier,
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
export const ignores = ['dist', 'node_modules']
|
package/src/ApiClient.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosResponse } from 'axios'
|
|
2
|
-
import { ZodSchema, ZodType, ZodTypeDef } from 'zod'
|
|
3
|
-
|
|
4
|
-
import { InputValidationError, OutputValidationError } from './errors'
|
|
5
|
-
import { ApiClientPlugin, ApiExtension, ZodInput, ZodOutput } from './types'
|
|
6
|
-
|
|
7
|
-
export abstract class AbstractApiRequest<
|
|
8
|
-
TInputSchema extends ZodType<any, ZodTypeDef, any>,
|
|
9
|
-
TOutputSchema extends ZodType<any, ZodTypeDef, any>,
|
|
10
|
-
Query = unknown,
|
|
11
|
-
Headers = unknown,
|
|
12
|
-
> {
|
|
13
|
-
abstract readonly method: 'GET' | 'POST' | 'PATCH' | 'DELETE'
|
|
14
|
-
abstract readonly contentType:
|
|
15
|
-
| 'application/json'
|
|
16
|
-
| 'multipart/form-data'
|
|
17
|
-
| 'application/merge-patch+json'
|
|
18
|
-
abstract readonly accept: 'application/json'
|
|
19
|
-
|
|
20
|
-
abstract readonly inputSchema: TInputSchema
|
|
21
|
-
abstract readonly outputSchema: TOutputSchema
|
|
22
|
-
abstract readonly querySchema?: ZodSchema<Query>
|
|
23
|
-
abstract readonly headersSchema?: ZodSchema<Headers>
|
|
24
|
-
|
|
25
|
-
protected constructor(
|
|
26
|
-
// eslint-disable-next-line
|
|
27
|
-
public readonly input: ZodInput<TInputSchema>,
|
|
28
|
-
// eslint-disable-next-line
|
|
29
|
-
public readonly options?: {
|
|
30
|
-
query?: Query
|
|
31
|
-
headers?: Headers
|
|
32
|
-
},
|
|
33
|
-
) {}
|
|
34
|
-
|
|
35
|
-
abstract getPath(): string
|
|
36
|
-
|
|
37
|
-
validateInput(): ZodOutput<TInputSchema> {
|
|
38
|
-
const result = this.inputSchema.safeParse(this.input)
|
|
39
|
-
if (!result.success) throw new InputValidationError(result.error.issues)
|
|
40
|
-
return result.data
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
validateQuery(): Query | undefined {
|
|
44
|
-
if (!this.querySchema || !this.options?.query) return undefined
|
|
45
|
-
const result = this.querySchema.safeParse(this.options.query)
|
|
46
|
-
if (!result.success) throw new InputValidationError(result.error.issues)
|
|
47
|
-
return result.data
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
validateHeaders(): Headers | undefined {
|
|
51
|
-
if (!this.headersSchema || !this.options?.headers) return undefined
|
|
52
|
-
const result = this.headersSchema.safeParse(this.options.headers)
|
|
53
|
-
if (!result.success) throw new InputValidationError(result.error.issues)
|
|
54
|
-
return result.data
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
validateOutput(data: unknown): ZodOutput<TOutputSchema> {
|
|
58
|
-
const result = this.outputSchema.safeParse(data)
|
|
59
|
-
if (!result.success) throw new OutputValidationError(result.error.issues)
|
|
60
|
-
return result.data
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
parseResponse(data: unknown, rawResponse?: AxiosResponse): ZodOutput<TOutputSchema> {
|
|
64
|
-
void rawResponse
|
|
65
|
-
return this.validateOutput(data)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
interface ApiClientBase {
|
|
70
|
-
http: AxiosInstance
|
|
71
|
-
|
|
72
|
-
call<
|
|
73
|
-
TInputSchema extends ZodType<any, ZodTypeDef, any>,
|
|
74
|
-
TOutputSchema extends ZodType<any, ZodTypeDef, any>,
|
|
75
|
-
Q = unknown,
|
|
76
|
-
H = unknown,
|
|
77
|
-
>(
|
|
78
|
-
// eslint-disable-next-line
|
|
79
|
-
request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>,
|
|
80
|
-
): Promise<ZodOutput<TOutputSchema>>
|
|
81
|
-
|
|
82
|
-
// eslint-disable-next-line
|
|
83
|
-
addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase &
|
|
87
|
-
Extensions
|
|
88
|
-
|
|
89
|
-
// --- Factory ---
|
|
90
|
-
export function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
|
|
91
|
-
baseUrl: string
|
|
92
|
-
}): ApiClient<Extensions> {
|
|
93
|
-
const http = axios.create({ baseURL: config.baseUrl })
|
|
94
|
-
|
|
95
|
-
const base: ApiClientBase = {
|
|
96
|
-
http,
|
|
97
|
-
|
|
98
|
-
async call<
|
|
99
|
-
TInputSchema extends ZodType<any, ZodTypeDef, any>,
|
|
100
|
-
TOutputSchema extends ZodType<any, ZodTypeDef, any>,
|
|
101
|
-
Q,
|
|
102
|
-
H,
|
|
103
|
-
>(
|
|
104
|
-
request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>,
|
|
105
|
-
): Promise<ZodOutput<TOutputSchema>> {
|
|
106
|
-
const input = request.validateInput()
|
|
107
|
-
const query = request.validateQuery()
|
|
108
|
-
const headers = request.validateHeaders()
|
|
109
|
-
|
|
110
|
-
const res = await http.request({
|
|
111
|
-
url: request.getPath(),
|
|
112
|
-
method: request.method,
|
|
113
|
-
headers: {
|
|
114
|
-
'Content-Type': request.contentType,
|
|
115
|
-
Accept: request.accept,
|
|
116
|
-
...(headers ?? {}),
|
|
117
|
-
},
|
|
118
|
-
params: query,
|
|
119
|
-
data: request.method !== 'GET' ? input : undefined,
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
return request.parseResponse(res.data, res)
|
|
123
|
-
},
|
|
124
|
-
|
|
125
|
-
addPlugin<T extends ApiExtension>(plugin: ApiClientPlugin<T>) {
|
|
126
|
-
const extension = plugin.setup(this as ApiClient<Extensions & T>)
|
|
127
|
-
return { ...this, ...extension } as ApiClient<Extensions & T>
|
|
128
|
-
},
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return base as ApiClient<Extensions>
|
|
132
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ZodIssue } from 'zod'
|
|
2
|
-
|
|
3
|
-
export class InputValidationError extends Error {
|
|
4
|
-
// eslint-disable-next-line no-unused-vars
|
|
5
|
-
constructor(public readonly issues: ZodIssue[]) {
|
|
6
|
-
super('Invalid input')
|
|
7
|
-
this.name = 'InputValidationError'
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class OutputValidationError extends Error {
|
|
12
|
-
// eslint-disable-next-line no-unused-vars
|
|
13
|
-
constructor(public readonly issues: ZodIssue[]) {
|
|
14
|
-
super('Invalid response')
|
|
15
|
-
this.name = 'OutputValidationError'
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/index.ts
DELETED
package/src/types.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ZodType } from 'zod'
|
|
2
|
-
|
|
3
|
-
import { ApiClient } from './ApiClient'
|
|
4
|
-
|
|
5
|
-
export type ApiExtension = Record<string, unknown>
|
|
6
|
-
|
|
7
|
-
export interface ApiClientPlugin<T extends ApiExtension = Record<string, unknown>> {
|
|
8
|
-
// eslint-disable-next-line no-unused-vars
|
|
9
|
-
setup: (client: ApiClient<Record<string, unknown>>) => T
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type ZodInput<T extends ZodType<any, any, any>> =
|
|
13
|
-
T extends ZodType<any, any, infer I> ? I : never
|
|
14
|
-
|
|
15
|
-
export type ZodOutput<T extends ZodType<any, any, any>> =
|
|
16
|
-
T extends ZodType<infer O, any, any> ? O : never
|
package/tsconfig.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "Node",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"forceConsistentCasingInFileNames": true,
|
|
8
|
-
"strict": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"outDir": "dist",
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"declarationMap": true
|
|
13
|
-
},
|
|
14
|
-
"include": ["src"]
|
|
15
|
-
}
|