@deliverart/sdk-js-core 0.1.4 → 0.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/CHANGELOG.md +6 -0
- package/dist/index.cjs +4 -12
- package/dist/index.d.cts +14 -12
- package/dist/index.d.ts +14 -12
- package/dist/index.js +4 -12
- package/package.json +1 -1
- package/src/ApiClient.ts +46 -33
- package/src/types.ts +8 -0
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -66,32 +66,24 @@ var AbstractApiRequest = class {
|
|
|
66
66
|
}
|
|
67
67
|
validateInput() {
|
|
68
68
|
const result = this.inputSchema.safeParse(this.input);
|
|
69
|
-
if (!result.success)
|
|
70
|
-
throw new InputValidationError(result.error.issues);
|
|
71
|
-
}
|
|
69
|
+
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
72
70
|
return result.data;
|
|
73
71
|
}
|
|
74
72
|
validateQuery() {
|
|
75
73
|
if (!this.querySchema || !this.options?.query) return void 0;
|
|
76
74
|
const result = this.querySchema.safeParse(this.options.query);
|
|
77
|
-
if (!result.success)
|
|
78
|
-
throw new InputValidationError(result.error.issues);
|
|
79
|
-
}
|
|
75
|
+
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
80
76
|
return result.data;
|
|
81
77
|
}
|
|
82
78
|
validateHeaders() {
|
|
83
79
|
if (!this.headersSchema || !this.options?.headers) return void 0;
|
|
84
80
|
const result = this.headersSchema.safeParse(this.options.headers);
|
|
85
|
-
if (!result.success)
|
|
86
|
-
throw new InputValidationError(result.error.issues);
|
|
87
|
-
}
|
|
81
|
+
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
88
82
|
return result.data;
|
|
89
83
|
}
|
|
90
84
|
validateOutput(data) {
|
|
91
85
|
const result = this.outputSchema.safeParse(data);
|
|
92
|
-
if (!result.success)
|
|
93
|
-
throw new OutputValidationError(result.error.issues);
|
|
94
|
-
}
|
|
86
|
+
if (!result.success) throw new OutputValidationError(result.error.issues);
|
|
95
87
|
return result.data;
|
|
96
88
|
}
|
|
97
89
|
parseResponse(data, rawResponse) {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { AxiosResponse, AxiosInstance } from 'axios';
|
|
2
|
-
import { ZodSchema, ZodIssue } from 'zod';
|
|
2
|
+
import { ZodType, ZodTypeDef, ZodSchema, ZodIssue } from 'zod';
|
|
3
3
|
|
|
4
4
|
type ApiExtension = Record<string, unknown>;
|
|
5
5
|
interface ApiClientPlugin<T extends ApiExtension = Record<string, unknown>> {
|
|
6
6
|
setup: (client: ApiClient<Record<string, unknown>>) => T;
|
|
7
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;
|
|
8
10
|
|
|
9
|
-
declare abstract class AbstractApiRequest<
|
|
10
|
-
readonly input:
|
|
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>;
|
|
11
13
|
readonly options?: {
|
|
12
14
|
query?: Query;
|
|
13
15
|
headers?: Headers;
|
|
@@ -15,30 +17,30 @@ declare abstract class AbstractApiRequest<Input, Output, Query = unknown, Header
|
|
|
15
17
|
abstract readonly method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
16
18
|
abstract readonly contentType: 'application/json' | 'multipart/form-data' | 'application/merge-patch+json';
|
|
17
19
|
abstract readonly accept: 'application/json';
|
|
18
|
-
abstract readonly inputSchema:
|
|
19
|
-
abstract readonly outputSchema:
|
|
20
|
+
abstract readonly inputSchema: TInputSchema;
|
|
21
|
+
abstract readonly outputSchema: TOutputSchema;
|
|
20
22
|
abstract readonly querySchema?: ZodSchema<Query>;
|
|
21
23
|
abstract readonly headersSchema?: ZodSchema<Headers>;
|
|
22
|
-
protected constructor(input:
|
|
24
|
+
protected constructor(input: ZodInput<TInputSchema>, options?: {
|
|
23
25
|
query?: Query;
|
|
24
26
|
headers?: Headers;
|
|
25
27
|
} | undefined);
|
|
26
28
|
abstract getPath(): string;
|
|
27
|
-
validateInput():
|
|
29
|
+
validateInput(): ZodOutput<TInputSchema>;
|
|
28
30
|
validateQuery(): Query | undefined;
|
|
29
31
|
validateHeaders(): Headers | undefined;
|
|
30
|
-
validateOutput(data: unknown):
|
|
31
|
-
parseResponse(data: unknown, rawResponse?: AxiosResponse):
|
|
32
|
+
validateOutput(data: unknown): ZodOutput<TOutputSchema>;
|
|
33
|
+
parseResponse(data: unknown, rawResponse?: AxiosResponse): ZodOutput<TOutputSchema>;
|
|
32
34
|
}
|
|
33
35
|
interface ApiClientBase {
|
|
34
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>>;
|
|
35
38
|
addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>;
|
|
36
|
-
call<I, O, Q, H>(request: AbstractApiRequest<I, O, Q, H>): Promise<O>;
|
|
37
39
|
}
|
|
40
|
+
type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
|
|
38
41
|
declare function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
|
|
39
42
|
baseUrl: string;
|
|
40
43
|
}): ApiClient<Extensions>;
|
|
41
|
-
type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
|
|
42
44
|
|
|
43
45
|
declare class InputValidationError extends Error {
|
|
44
46
|
readonly issues: ZodIssue[];
|
|
@@ -49,4 +51,4 @@ declare class OutputValidationError extends Error {
|
|
|
49
51
|
constructor(issues: ZodIssue[]);
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, InputValidationError, OutputValidationError, createApiClient };
|
|
54
|
+
export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, InputValidationError, OutputValidationError, type ZodInput, type ZodOutput, createApiClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { AxiosResponse, AxiosInstance } from 'axios';
|
|
2
|
-
import { ZodSchema, ZodIssue } from 'zod';
|
|
2
|
+
import { ZodType, ZodTypeDef, ZodSchema, ZodIssue } from 'zod';
|
|
3
3
|
|
|
4
4
|
type ApiExtension = Record<string, unknown>;
|
|
5
5
|
interface ApiClientPlugin<T extends ApiExtension = Record<string, unknown>> {
|
|
6
6
|
setup: (client: ApiClient<Record<string, unknown>>) => T;
|
|
7
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;
|
|
8
10
|
|
|
9
|
-
declare abstract class AbstractApiRequest<
|
|
10
|
-
readonly input:
|
|
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>;
|
|
11
13
|
readonly options?: {
|
|
12
14
|
query?: Query;
|
|
13
15
|
headers?: Headers;
|
|
@@ -15,30 +17,30 @@ declare abstract class AbstractApiRequest<Input, Output, Query = unknown, Header
|
|
|
15
17
|
abstract readonly method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
16
18
|
abstract readonly contentType: 'application/json' | 'multipart/form-data' | 'application/merge-patch+json';
|
|
17
19
|
abstract readonly accept: 'application/json';
|
|
18
|
-
abstract readonly inputSchema:
|
|
19
|
-
abstract readonly outputSchema:
|
|
20
|
+
abstract readonly inputSchema: TInputSchema;
|
|
21
|
+
abstract readonly outputSchema: TOutputSchema;
|
|
20
22
|
abstract readonly querySchema?: ZodSchema<Query>;
|
|
21
23
|
abstract readonly headersSchema?: ZodSchema<Headers>;
|
|
22
|
-
protected constructor(input:
|
|
24
|
+
protected constructor(input: ZodInput<TInputSchema>, options?: {
|
|
23
25
|
query?: Query;
|
|
24
26
|
headers?: Headers;
|
|
25
27
|
} | undefined);
|
|
26
28
|
abstract getPath(): string;
|
|
27
|
-
validateInput():
|
|
29
|
+
validateInput(): ZodOutput<TInputSchema>;
|
|
28
30
|
validateQuery(): Query | undefined;
|
|
29
31
|
validateHeaders(): Headers | undefined;
|
|
30
|
-
validateOutput(data: unknown):
|
|
31
|
-
parseResponse(data: unknown, rawResponse?: AxiosResponse):
|
|
32
|
+
validateOutput(data: unknown): ZodOutput<TOutputSchema>;
|
|
33
|
+
parseResponse(data: unknown, rawResponse?: AxiosResponse): ZodOutput<TOutputSchema>;
|
|
32
34
|
}
|
|
33
35
|
interface ApiClientBase {
|
|
34
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>>;
|
|
35
38
|
addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>;
|
|
36
|
-
call<I, O, Q, H>(request: AbstractApiRequest<I, O, Q, H>): Promise<O>;
|
|
37
39
|
}
|
|
40
|
+
type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
|
|
38
41
|
declare function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
|
|
39
42
|
baseUrl: string;
|
|
40
43
|
}): ApiClient<Extensions>;
|
|
41
|
-
type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
|
|
42
44
|
|
|
43
45
|
declare class InputValidationError extends Error {
|
|
44
46
|
readonly issues: ZodIssue[];
|
|
@@ -49,4 +51,4 @@ declare class OutputValidationError extends Error {
|
|
|
49
51
|
constructor(issues: ZodIssue[]);
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, InputValidationError, OutputValidationError, createApiClient };
|
|
54
|
+
export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, InputValidationError, OutputValidationError, type ZodInput, type ZodOutput, createApiClient };
|
package/dist/index.js
CHANGED
|
@@ -27,32 +27,24 @@ var AbstractApiRequest = class {
|
|
|
27
27
|
}
|
|
28
28
|
validateInput() {
|
|
29
29
|
const result = this.inputSchema.safeParse(this.input);
|
|
30
|
-
if (!result.success)
|
|
31
|
-
throw new InputValidationError(result.error.issues);
|
|
32
|
-
}
|
|
30
|
+
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
33
31
|
return result.data;
|
|
34
32
|
}
|
|
35
33
|
validateQuery() {
|
|
36
34
|
if (!this.querySchema || !this.options?.query) return void 0;
|
|
37
35
|
const result = this.querySchema.safeParse(this.options.query);
|
|
38
|
-
if (!result.success)
|
|
39
|
-
throw new InputValidationError(result.error.issues);
|
|
40
|
-
}
|
|
36
|
+
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
41
37
|
return result.data;
|
|
42
38
|
}
|
|
43
39
|
validateHeaders() {
|
|
44
40
|
if (!this.headersSchema || !this.options?.headers) return void 0;
|
|
45
41
|
const result = this.headersSchema.safeParse(this.options.headers);
|
|
46
|
-
if (!result.success)
|
|
47
|
-
throw new InputValidationError(result.error.issues);
|
|
48
|
-
}
|
|
42
|
+
if (!result.success) throw new InputValidationError(result.error.issues);
|
|
49
43
|
return result.data;
|
|
50
44
|
}
|
|
51
45
|
validateOutput(data) {
|
|
52
46
|
const result = this.outputSchema.safeParse(data);
|
|
53
|
-
if (!result.success)
|
|
54
|
-
throw new OutputValidationError(result.error.issues);
|
|
55
|
-
}
|
|
47
|
+
if (!result.success) throw new OutputValidationError(result.error.issues);
|
|
56
48
|
return result.data;
|
|
57
49
|
}
|
|
58
50
|
parseResponse(data, rawResponse) {
|
package/package.json
CHANGED
package/src/ApiClient.ts
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
import axios, { AxiosInstance, AxiosResponse } from 'axios'
|
|
2
|
-
import { ZodSchema } from 'zod'
|
|
2
|
+
import { ZodSchema, ZodType, ZodTypeDef } from 'zod'
|
|
3
3
|
|
|
4
4
|
import { InputValidationError, OutputValidationError } from './errors'
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
export abstract class AbstractApiRequest<
|
|
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
|
+
> {
|
|
8
13
|
abstract readonly method: 'GET' | 'POST' | 'PATCH' | 'DELETE'
|
|
9
14
|
abstract readonly contentType:
|
|
10
15
|
| 'application/json'
|
|
11
16
|
| 'multipart/form-data'
|
|
12
17
|
| 'application/merge-patch+json'
|
|
13
18
|
abstract readonly accept: 'application/json'
|
|
14
|
-
|
|
15
|
-
abstract readonly
|
|
19
|
+
|
|
20
|
+
abstract readonly inputSchema: TInputSchema
|
|
21
|
+
abstract readonly outputSchema: TOutputSchema
|
|
16
22
|
abstract readonly querySchema?: ZodSchema<Query>
|
|
17
23
|
abstract readonly headersSchema?: ZodSchema<Headers>
|
|
18
24
|
|
|
19
25
|
protected constructor(
|
|
20
|
-
// eslint-disable-next-line
|
|
21
|
-
public readonly input:
|
|
22
|
-
// eslint-disable-next-line
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
public readonly input: ZodInput<TInputSchema>,
|
|
28
|
+
// eslint-disable-next-line
|
|
23
29
|
public readonly options?: {
|
|
24
30
|
query?: Query
|
|
25
31
|
headers?: Headers
|
|
@@ -28,43 +34,33 @@ export abstract class AbstractApiRequest<Input, Output, Query = unknown, Headers
|
|
|
28
34
|
|
|
29
35
|
abstract getPath(): string
|
|
30
36
|
|
|
31
|
-
validateInput():
|
|
37
|
+
validateInput(): ZodOutput<TInputSchema> {
|
|
32
38
|
const result = this.inputSchema.safeParse(this.input)
|
|
33
|
-
if (!result.success)
|
|
34
|
-
throw new InputValidationError(result.error.issues)
|
|
35
|
-
}
|
|
39
|
+
if (!result.success) throw new InputValidationError(result.error.issues)
|
|
36
40
|
return result.data
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
validateQuery(): Query | undefined {
|
|
40
44
|
if (!this.querySchema || !this.options?.query) return undefined
|
|
41
|
-
|
|
42
45
|
const result = this.querySchema.safeParse(this.options.query)
|
|
43
|
-
if (!result.success)
|
|
44
|
-
throw new InputValidationError(result.error.issues)
|
|
45
|
-
}
|
|
46
|
+
if (!result.success) throw new InputValidationError(result.error.issues)
|
|
46
47
|
return result.data
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
validateHeaders(): Headers | undefined {
|
|
50
51
|
if (!this.headersSchema || !this.options?.headers) return undefined
|
|
51
|
-
|
|
52
52
|
const result = this.headersSchema.safeParse(this.options.headers)
|
|
53
|
-
if (!result.success)
|
|
54
|
-
throw new InputValidationError(result.error.issues)
|
|
55
|
-
}
|
|
53
|
+
if (!result.success) throw new InputValidationError(result.error.issues)
|
|
56
54
|
return result.data
|
|
57
55
|
}
|
|
58
56
|
|
|
59
|
-
validateOutput(data: unknown):
|
|
57
|
+
validateOutput(data: unknown): ZodOutput<TOutputSchema> {
|
|
60
58
|
const result = this.outputSchema.safeParse(data)
|
|
61
|
-
if (!result.success)
|
|
62
|
-
throw new OutputValidationError(result.error.issues)
|
|
63
|
-
}
|
|
59
|
+
if (!result.success) throw new OutputValidationError(result.error.issues)
|
|
64
60
|
return result.data
|
|
65
61
|
}
|
|
66
62
|
|
|
67
|
-
parseResponse(data: unknown, rawResponse?: AxiosResponse):
|
|
63
|
+
parseResponse(data: unknown, rawResponse?: AxiosResponse): ZodOutput<TOutputSchema> {
|
|
68
64
|
void rawResponse
|
|
69
65
|
return this.validateOutput(data)
|
|
70
66
|
}
|
|
@@ -72,12 +68,25 @@ export abstract class AbstractApiRequest<Input, Output, Query = unknown, Headers
|
|
|
72
68
|
|
|
73
69
|
interface ApiClientBase {
|
|
74
70
|
http: AxiosInstance
|
|
75
|
-
|
|
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
|
|
76
83
|
addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>
|
|
77
|
-
// eslint-disable-next-line no-unused-vars
|
|
78
|
-
call<I, O, Q, H>(request: AbstractApiRequest<I, O, Q, H>): Promise<O>
|
|
79
84
|
}
|
|
80
85
|
|
|
86
|
+
export type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase &
|
|
87
|
+
Extensions
|
|
88
|
+
|
|
89
|
+
// --- Factory ---
|
|
81
90
|
export function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
|
|
82
91
|
baseUrl: string
|
|
83
92
|
}): ApiClient<Extensions> {
|
|
@@ -86,7 +95,14 @@ export function createApiClient<Extensions extends ApiExtension = NonNullable<un
|
|
|
86
95
|
const base: ApiClientBase = {
|
|
87
96
|
http,
|
|
88
97
|
|
|
89
|
-
async call<
|
|
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>> {
|
|
90
106
|
const input = request.validateInput()
|
|
91
107
|
const query = request.validateQuery()
|
|
92
108
|
const headers = request.validateHeaders()
|
|
@@ -114,6 +130,3 @@ export function createApiClient<Extensions extends ApiExtension = NonNullable<un
|
|
|
114
130
|
|
|
115
131
|
return base as ApiClient<Extensions>
|
|
116
132
|
}
|
|
117
|
-
|
|
118
|
-
export type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase &
|
|
119
|
-
Extensions
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ZodType } from 'zod'
|
|
2
|
+
|
|
1
3
|
import { ApiClient } from './ApiClient'
|
|
2
4
|
|
|
3
5
|
export type ApiExtension = Record<string, unknown>
|
|
@@ -6,3 +8,9 @@ export interface ApiClientPlugin<T extends ApiExtension = Record<string, unknown
|
|
|
6
8
|
// eslint-disable-next-line no-unused-vars
|
|
7
9
|
setup: (client: ApiClient<Record<string, unknown>>) => T
|
|
8
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
|