@deliverart/sdk-js-core 0.0.0-dev-20260721002647

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.
@@ -0,0 +1,125 @@
1
+ import { ZodType, input, output, ZodError, z } from 'zod';
2
+ import { IriObject } from '@deliverart/sdk-js-global-types';
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
+
9
+ type UnpaginatedResponse<T> = T extends {
10
+ data: infer U;
11
+ } ? U : T;
12
+ declare abstract class AbstractApiRequest<TInputSchema extends ZodType<any, any, any>, TOutputSchema extends ZodType<any, any, any>, Query = unknown, Headers = unknown> {
13
+ readonly input: input<TInputSchema>;
14
+ readonly options?: {
15
+ query?: Query;
16
+ headers?: Headers;
17
+ /**
18
+ * Toggle pagination at call-time if the request supports it.
19
+ * - true: attach pagination (if `getPagination` returns a value)
20
+ * - false: return a plain array of items
21
+ * - undefined: fall back to `paginationDefaultEnabled` (true if not set)
22
+ */
23
+ pagination?: boolean;
24
+ } | undefined;
25
+ abstract readonly method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
26
+ abstract readonly contentType: 'application/json' | 'multipart/form-data' | 'application/merge-patch+json';
27
+ abstract readonly accept: 'application/json';
28
+ abstract readonly inputSchema: TInputSchema;
29
+ abstract readonly outputSchema: TOutputSchema;
30
+ abstract readonly querySchema?: ZodType<Query>;
31
+ abstract readonly headersSchema?: ZodType<Headers>;
32
+ /**
33
+ * Optional hints to enable automatic list parsing.
34
+ * - If `listItemSchema` is provided, `parseResponse` will automatically
35
+ * parse arrays and optionally attach pagination when enabled.
36
+ * - Pagination attachment requires overriding `getPagination` in concrete requests
37
+ * to compute pagination metadata from the raw response (e.g. headers).
38
+ */
39
+ readonly listItemSchema?: ZodType<any, any, any>;
40
+ /** Whether pagination is enabled by default for this request (if available). */
41
+ readonly paginationDefaultEnabled?: boolean;
42
+ protected constructor(input: input<TInputSchema>, options?: {
43
+ query?: Query;
44
+ headers?: Headers;
45
+ /**
46
+ * Toggle pagination at call-time if the request supports it.
47
+ * - true: attach pagination (if `getPagination` returns a value)
48
+ * - false: return a plain array of items
49
+ * - undefined: fall back to `paginationDefaultEnabled` (true if not set)
50
+ */
51
+ pagination?: boolean;
52
+ } | undefined);
53
+ abstract getPath(): string;
54
+ /**
55
+ * Override this in list requests to provide pagination metadata from the raw HTTP response.
56
+ * Default implementation uses `responseToPagination`.
57
+ * Return `undefined` if pagination cannot be resolved.
58
+ */
59
+ protected getPagination(rawResponse: Response): unknown | undefined;
60
+ validateInput(): output<TInputSchema>;
61
+ validateQuery(): Query | undefined;
62
+ validateHeaders(): Headers | undefined;
63
+ validateOutput(data: unknown, useArraySchema?: boolean): output<TOutputSchema>;
64
+ parseResponse(data: unknown, rawResponse?: Response, paginationOption?: boolean): output<TOutputSchema>;
65
+ }
66
+ type RequestContext = {
67
+ url: URL;
68
+ init: RequestInit;
69
+ };
70
+ type RequestMiddleware = (ctx: RequestContext) => Promise<RequestContext> | RequestContext;
71
+ type ParsedResponse = {
72
+ response: Response;
73
+ body: unknown;
74
+ };
75
+ type ResponseMiddleware = (parsed: ParsedResponse, requestCtx: RequestContext) => Promise<ParsedResponse> | ParsedResponse;
76
+ interface ApiClientBase {
77
+ fetch: typeof fetch;
78
+ call<TInputSchema extends ZodType<any, any, any>, TOutputSchema extends ZodType<any, any, any>, Q = unknown, H = unknown>(request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>, options: {
79
+ pagination: false;
80
+ itemsPerPage?: number;
81
+ }): Promise<UnpaginatedResponse<output<TOutputSchema>>>;
82
+ call<TInputSchema extends ZodType<any, any, any>, TOutputSchema extends ZodType<any, any, any>, Q = unknown, H = unknown>(request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>, options?: {
83
+ pagination?: true;
84
+ itemsPerPage?: number;
85
+ }): Promise<output<TOutputSchema>>;
86
+ addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>;
87
+ addRequestMiddleware: (middleware: RequestMiddleware) => void;
88
+ addResponseMiddleware: (middleware: ResponseMiddleware) => void;
89
+ }
90
+ type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
91
+ declare function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
92
+ baseUrl: string;
93
+ }): ApiClient<Extensions>;
94
+
95
+ type ZodIssue = ZodError['issues'][number];
96
+ declare class InputValidationError extends Error {
97
+ readonly issues: ZodIssue[];
98
+ constructor(issues: ZodIssue[]);
99
+ }
100
+ declare class OutputValidationError extends Error {
101
+ readonly issues: ZodIssue[];
102
+ constructor(issues: ZodIssue[]);
103
+ }
104
+
105
+ declare const serializedIriObjectSchema: z.ZodObject<{
106
+ iri: z.ZodString;
107
+ params: z.ZodRecord<z.ZodString, z.ZodString>;
108
+ __pattern: z.ZodString;
109
+ }, z.core.$strict>;
110
+ type SerializedIriObject<TPattern extends string = string> = {
111
+ iri: string;
112
+ params: Record<string, string>;
113
+ __pattern: TPattern;
114
+ };
115
+ type DeepDeserialized<T> = T extends SerializedIriObject<infer P> ? IriObject<P> : T extends Array<infer U> ? DeepDeserialized<U>[] : T extends object ? {
116
+ [K in keyof T]: DeepDeserialized<T[K]>;
117
+ } : T;
118
+ type DeepSerialized<T> = T extends IriObject<infer P> ? SerializedIriObject<P> : T extends Array<infer U> ? DeepSerialized<U>[] : T extends object ? {
119
+ [K in keyof T]: DeepSerialized<T[K]>;
120
+ } : T;
121
+ declare function serializeForClient<T>(input: T): DeepSerialized<T>;
122
+ declare function deserializeFromClient<T extends SerializedIriObject<P>, P extends string>(input: T): IriObject<P>;
123
+ declare function deserializeFromClient<T>(input: T): DeepDeserialized<T>;
124
+
125
+ export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, type DeepDeserialized, type DeepSerialized, InputValidationError, OutputValidationError, type SerializedIriObject, type UnpaginatedResponse, createApiClient, deserializeFromClient, serializeForClient, serializedIriObjectSchema };
@@ -0,0 +1,125 @@
1
+ import { ZodType, input, output, ZodError, z } from 'zod';
2
+ import { IriObject } from '@deliverart/sdk-js-global-types';
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
+
9
+ type UnpaginatedResponse<T> = T extends {
10
+ data: infer U;
11
+ } ? U : T;
12
+ declare abstract class AbstractApiRequest<TInputSchema extends ZodType<any, any, any>, TOutputSchema extends ZodType<any, any, any>, Query = unknown, Headers = unknown> {
13
+ readonly input: input<TInputSchema>;
14
+ readonly options?: {
15
+ query?: Query;
16
+ headers?: Headers;
17
+ /**
18
+ * Toggle pagination at call-time if the request supports it.
19
+ * - true: attach pagination (if `getPagination` returns a value)
20
+ * - false: return a plain array of items
21
+ * - undefined: fall back to `paginationDefaultEnabled` (true if not set)
22
+ */
23
+ pagination?: boolean;
24
+ } | undefined;
25
+ abstract readonly method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
26
+ abstract readonly contentType: 'application/json' | 'multipart/form-data' | 'application/merge-patch+json';
27
+ abstract readonly accept: 'application/json';
28
+ abstract readonly inputSchema: TInputSchema;
29
+ abstract readonly outputSchema: TOutputSchema;
30
+ abstract readonly querySchema?: ZodType<Query>;
31
+ abstract readonly headersSchema?: ZodType<Headers>;
32
+ /**
33
+ * Optional hints to enable automatic list parsing.
34
+ * - If `listItemSchema` is provided, `parseResponse` will automatically
35
+ * parse arrays and optionally attach pagination when enabled.
36
+ * - Pagination attachment requires overriding `getPagination` in concrete requests
37
+ * to compute pagination metadata from the raw response (e.g. headers).
38
+ */
39
+ readonly listItemSchema?: ZodType<any, any, any>;
40
+ /** Whether pagination is enabled by default for this request (if available). */
41
+ readonly paginationDefaultEnabled?: boolean;
42
+ protected constructor(input: input<TInputSchema>, options?: {
43
+ query?: Query;
44
+ headers?: Headers;
45
+ /**
46
+ * Toggle pagination at call-time if the request supports it.
47
+ * - true: attach pagination (if `getPagination` returns a value)
48
+ * - false: return a plain array of items
49
+ * - undefined: fall back to `paginationDefaultEnabled` (true if not set)
50
+ */
51
+ pagination?: boolean;
52
+ } | undefined);
53
+ abstract getPath(): string;
54
+ /**
55
+ * Override this in list requests to provide pagination metadata from the raw HTTP response.
56
+ * Default implementation uses `responseToPagination`.
57
+ * Return `undefined` if pagination cannot be resolved.
58
+ */
59
+ protected getPagination(rawResponse: Response): unknown | undefined;
60
+ validateInput(): output<TInputSchema>;
61
+ validateQuery(): Query | undefined;
62
+ validateHeaders(): Headers | undefined;
63
+ validateOutput(data: unknown, useArraySchema?: boolean): output<TOutputSchema>;
64
+ parseResponse(data: unknown, rawResponse?: Response, paginationOption?: boolean): output<TOutputSchema>;
65
+ }
66
+ type RequestContext = {
67
+ url: URL;
68
+ init: RequestInit;
69
+ };
70
+ type RequestMiddleware = (ctx: RequestContext) => Promise<RequestContext> | RequestContext;
71
+ type ParsedResponse = {
72
+ response: Response;
73
+ body: unknown;
74
+ };
75
+ type ResponseMiddleware = (parsed: ParsedResponse, requestCtx: RequestContext) => Promise<ParsedResponse> | ParsedResponse;
76
+ interface ApiClientBase {
77
+ fetch: typeof fetch;
78
+ call<TInputSchema extends ZodType<any, any, any>, TOutputSchema extends ZodType<any, any, any>, Q = unknown, H = unknown>(request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>, options: {
79
+ pagination: false;
80
+ itemsPerPage?: number;
81
+ }): Promise<UnpaginatedResponse<output<TOutputSchema>>>;
82
+ call<TInputSchema extends ZodType<any, any, any>, TOutputSchema extends ZodType<any, any, any>, Q = unknown, H = unknown>(request: AbstractApiRequest<TInputSchema, TOutputSchema, Q, H>, options?: {
83
+ pagination?: true;
84
+ itemsPerPage?: number;
85
+ }): Promise<output<TOutputSchema>>;
86
+ addPlugin: <T extends ApiExtension>(plugin: ApiClientPlugin<T>) => ApiClient<T>;
87
+ addRequestMiddleware: (middleware: RequestMiddleware) => void;
88
+ addResponseMiddleware: (middleware: ResponseMiddleware) => void;
89
+ }
90
+ type ApiClient<Extensions extends ApiExtension = NonNullable<unknown>> = ApiClientBase & Extensions;
91
+ declare function createApiClient<Extensions extends ApiExtension = NonNullable<unknown>>(config: {
92
+ baseUrl: string;
93
+ }): ApiClient<Extensions>;
94
+
95
+ type ZodIssue = ZodError['issues'][number];
96
+ declare class InputValidationError extends Error {
97
+ readonly issues: ZodIssue[];
98
+ constructor(issues: ZodIssue[]);
99
+ }
100
+ declare class OutputValidationError extends Error {
101
+ readonly issues: ZodIssue[];
102
+ constructor(issues: ZodIssue[]);
103
+ }
104
+
105
+ declare const serializedIriObjectSchema: z.ZodObject<{
106
+ iri: z.ZodString;
107
+ params: z.ZodRecord<z.ZodString, z.ZodString>;
108
+ __pattern: z.ZodString;
109
+ }, z.core.$strict>;
110
+ type SerializedIriObject<TPattern extends string = string> = {
111
+ iri: string;
112
+ params: Record<string, string>;
113
+ __pattern: TPattern;
114
+ };
115
+ type DeepDeserialized<T> = T extends SerializedIriObject<infer P> ? IriObject<P> : T extends Array<infer U> ? DeepDeserialized<U>[] : T extends object ? {
116
+ [K in keyof T]: DeepDeserialized<T[K]>;
117
+ } : T;
118
+ type DeepSerialized<T> = T extends IriObject<infer P> ? SerializedIriObject<P> : T extends Array<infer U> ? DeepSerialized<U>[] : T extends object ? {
119
+ [K in keyof T]: DeepSerialized<T[K]>;
120
+ } : T;
121
+ declare function serializeForClient<T>(input: T): DeepSerialized<T>;
122
+ declare function deserializeFromClient<T extends SerializedIriObject<P>, P extends string>(input: T): IriObject<P>;
123
+ declare function deserializeFromClient<T>(input: T): DeepDeserialized<T>;
124
+
125
+ export { AbstractApiRequest, type ApiClient, type ApiClientPlugin, type ApiExtension, type DeepDeserialized, type DeepSerialized, InputValidationError, OutputValidationError, type SerializedIriObject, type UnpaginatedResponse, createApiClient, deserializeFromClient, serializeForClient, serializedIriObjectSchema };