@remkoj/hey-api-wrapper 6.0.0-rc.2
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/AGENTS.md +6 -0
- package/README.md +1 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +206 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +73 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +33 -0
package/AGENTS.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# hey-api-wrapper
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
export type { OperationsList } from './types';
|
|
2
|
+
import type { ApiClientConfig, ApiClientFunctions, ClassWithMixin, OperationsList } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Mixin factory that extends an {@link ApiClient} subclass with one method per
|
|
5
|
+
* hey-api operation. Each generated method calls the matching operation,
|
|
6
|
+
* injecting the instance's network client and `throwOnError: false`, throws an
|
|
7
|
+
* {@link ApiError} when the operation reports an error, and otherwise returns
|
|
8
|
+
* the response `data` (falling back to the full result).
|
|
9
|
+
*
|
|
10
|
+
* @param Base The {@link ApiClient} subclass to extend.
|
|
11
|
+
* @param Operations Map of hey-api operation functions to bind as methods.
|
|
12
|
+
* @returns A new class combining `Base` with the bound operation methods.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import * as SdkOps from './client/sdk.gen';
|
|
17
|
+
* import { createClient } from '@hey-api/client-fetch';
|
|
18
|
+
*
|
|
19
|
+
* class MyCmsApiClient extends withOperations(ApiClient, SdkOps) {
|
|
20
|
+
* constructor(config: ApiClientConfig, client: ReturnType<typeof createClient>) {
|
|
21
|
+
* super(config, client);
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* const api = new MyCmsApiClient({ debug: true }, createClient({ baseUrl: 'https://cms.example.com' }));
|
|
26
|
+
* const result = await api.listContent({ query: { pageSize: 10 } });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function withOperations<TBase extends ApiClientStatic, TOperations extends OperationsList>(Base: TBase, Operations: TOperations): ClassWithMixin<TBase, ApiClientFunctions<TOperations>>;
|
|
30
|
+
/**
|
|
31
|
+
* Minimal network client contract required by {@link ApiClient}: a client
|
|
32
|
+
* exposing request and response interceptor registration (as provided by
|
|
33
|
+
* `@hey-api/client-fetch`).
|
|
34
|
+
*/
|
|
35
|
+
export type ApiClientNetworkClient = {
|
|
36
|
+
interceptors: {
|
|
37
|
+
request: {
|
|
38
|
+
use: (interceptor: (request: Request) => Request | Promise<Request>) => number;
|
|
39
|
+
};
|
|
40
|
+
response: {
|
|
41
|
+
use: (interceptor: (response: Response, request: Request) => Response | Promise<Response>) => number;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* The static (constructor) side of an {@link ApiClient} subclass. Used as the
|
|
47
|
+
* `Base` constraint for {@link withOperations} so a concrete client class can be
|
|
48
|
+
* passed despite its narrowed constructor signature.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam C Configuration type, extending {@link ApiClientConfig}.
|
|
51
|
+
* @typeParam NC Network client type, extending {@link ApiClientNetworkClient}.
|
|
52
|
+
*/
|
|
53
|
+
export type ApiClientStatic<C extends ApiClientConfig = ApiClientConfig, NC extends ApiClientNetworkClient = ApiClientNetworkClient> = {
|
|
54
|
+
new (...args: any[]): ApiClient<C, NC>;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Abstract base for hey-api backed clients. Holds the immutable configuration
|
|
58
|
+
* and network client, and—when `debug` is enabled—installs request/response
|
|
59
|
+
* logging interceptors. Operation methods are added by {@link withOperations}.
|
|
60
|
+
*
|
|
61
|
+
* @typeParam C Configuration type, extending {@link ApiClientConfig}.
|
|
62
|
+
* @typeParam NC Network client type, extending {@link ApiClientNetworkClient}.
|
|
63
|
+
*/
|
|
64
|
+
export declare abstract class ApiClient<C extends ApiClientConfig = ApiClientConfig, NC extends ApiClientNetworkClient = ApiClientNetworkClient> {
|
|
65
|
+
/**
|
|
66
|
+
* Immutable configuration snapshot for this client instance.
|
|
67
|
+
* Accessible to subclasses only.
|
|
68
|
+
*/
|
|
69
|
+
protected readonly _config: Readonly<C>;
|
|
70
|
+
/**
|
|
71
|
+
* The `@hey-api` network client bound to this instance.
|
|
72
|
+
* Injected into every operation call. Accessible to subclasses only.
|
|
73
|
+
*/
|
|
74
|
+
protected readonly _client: NC;
|
|
75
|
+
/** Whether debug logging is enabled for this client. */
|
|
76
|
+
get debug(): boolean;
|
|
77
|
+
/** The configured client name, or `'API Client'` when unset. */
|
|
78
|
+
get name(): string;
|
|
79
|
+
/** The underlying network client used to perform operations. */
|
|
80
|
+
get client(): NC;
|
|
81
|
+
/**
|
|
82
|
+
* @param config Immutable client configuration.
|
|
83
|
+
* @param client Network client used to perform operations; receives logging interceptors when `config.debug` is set.
|
|
84
|
+
*/
|
|
85
|
+
constructor(config: C, client: NC);
|
|
86
|
+
/**
|
|
87
|
+
* Type guard for a hey-api error result (an object carrying `error`,
|
|
88
|
+
* `request` and `response`).
|
|
89
|
+
*
|
|
90
|
+
* @param toTest The value to inspect.
|
|
91
|
+
* @returns `true` when `toTest` is an error response.
|
|
92
|
+
*/
|
|
93
|
+
protected isErrorResponse(toTest?: unknown): toTest is {
|
|
94
|
+
error: unknown;
|
|
95
|
+
request: Request;
|
|
96
|
+
response: Response;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Type guard for a hey-api success result (an object carrying `data`,
|
|
100
|
+
* `request` and `response`).
|
|
101
|
+
*
|
|
102
|
+
* @param toTest The value to inspect.
|
|
103
|
+
* @returns `true` when `toTest` is a data response.
|
|
104
|
+
*/
|
|
105
|
+
protected isDataResponse<RT = unknown>(toTest?: unknown): toTest is {
|
|
106
|
+
data: RT & {};
|
|
107
|
+
request: Request;
|
|
108
|
+
response: Response;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Type guard for a non-null object.
|
|
112
|
+
*
|
|
113
|
+
* @param toTest The value to inspect.
|
|
114
|
+
* @returns `true` when `toTest` is a non-null object.
|
|
115
|
+
*/
|
|
116
|
+
protected isObject(toTest?: unknown): toTest is Record<string | number | symbol, unknown>;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Error thrown when an API operation returns an error result. Wraps the
|
|
120
|
+
* originating error payload together with the HTTP request and response for
|
|
121
|
+
* inspection.
|
|
122
|
+
*/
|
|
123
|
+
export declare class ApiError extends Error {
|
|
124
|
+
/** Raw error context captured from the failed operation: the error payload and the originating HTTP request and response. */
|
|
125
|
+
protected _ctx: {
|
|
126
|
+
error?: unknown;
|
|
127
|
+
request?: Request;
|
|
128
|
+
response?: Response;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* @param data The operation error context: the error payload plus the HTTP request and response. A string error is used verbatim as the message; otherwise the message is derived from the response status.
|
|
132
|
+
*/
|
|
133
|
+
constructor(data: {
|
|
134
|
+
error?: unknown;
|
|
135
|
+
request?: Request;
|
|
136
|
+
response?: Response;
|
|
137
|
+
});
|
|
138
|
+
/** The error payload returned by the operation. */
|
|
139
|
+
get data(): unknown;
|
|
140
|
+
/**
|
|
141
|
+
* @deprecated Use {@link data} instead.
|
|
142
|
+
*/
|
|
143
|
+
get body(): unknown;
|
|
144
|
+
/** The HTTP request that produced the error. */
|
|
145
|
+
get request(): unknown;
|
|
146
|
+
/** The HTTP response that produced the error. */
|
|
147
|
+
get response(): unknown;
|
|
148
|
+
/** The HTTP status code of the response. */
|
|
149
|
+
get status(): number;
|
|
150
|
+
/** The HTTP status text of the response. */
|
|
151
|
+
get statusText(): string;
|
|
152
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiError = exports.ApiClient = void 0;
|
|
4
|
+
exports.withOperations = withOperations;
|
|
5
|
+
/**
|
|
6
|
+
* Mixin factory that extends an {@link ApiClient} subclass with one method per
|
|
7
|
+
* hey-api operation. Each generated method calls the matching operation,
|
|
8
|
+
* injecting the instance's network client and `throwOnError: false`, throws an
|
|
9
|
+
* {@link ApiError} when the operation reports an error, and otherwise returns
|
|
10
|
+
* the response `data` (falling back to the full result).
|
|
11
|
+
*
|
|
12
|
+
* @param Base The {@link ApiClient} subclass to extend.
|
|
13
|
+
* @param Operations Map of hey-api operation functions to bind as methods.
|
|
14
|
+
* @returns A new class combining `Base` with the bound operation methods.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import * as SdkOps from './client/sdk.gen';
|
|
19
|
+
* import { createClient } from '@hey-api/client-fetch';
|
|
20
|
+
*
|
|
21
|
+
* class MyCmsApiClient extends withOperations(ApiClient, SdkOps) {
|
|
22
|
+
* constructor(config: ApiClientConfig, client: ReturnType<typeof createClient>) {
|
|
23
|
+
* super(config, client);
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* const api = new MyCmsApiClient({ debug: true }, createClient({ baseUrl: 'https://cms.example.com' }));
|
|
28
|
+
* const result = await api.listContent({ query: { pageSize: 10 } });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function withOperations(Base, Operations) {
|
|
32
|
+
//@ts-expect-error A mixin requires an ...any[] argument, but our concrete class has
|
|
33
|
+
// specified
|
|
34
|
+
class NewClass extends Base {
|
|
35
|
+
constructor(...args) {
|
|
36
|
+
super(...args);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Create guard for this list of Operations
|
|
40
|
+
const isApiClientFunction = createIsFunctionValidator(Operations);
|
|
41
|
+
// Bind the operations
|
|
42
|
+
;
|
|
43
|
+
Object.getOwnPropertyNames(Operations).filter(isApiClientFunction).forEach(propName => {
|
|
44
|
+
async function wrapper(...args) {
|
|
45
|
+
const operationArgs = [...args];
|
|
46
|
+
operationArgs[0] = {
|
|
47
|
+
throwOnError: false,
|
|
48
|
+
...args,
|
|
49
|
+
client: this._client
|
|
50
|
+
};
|
|
51
|
+
//@ts-expect-error TypeScript can't check this as TOperations is dynamic
|
|
52
|
+
const result = await Operations[propName](...operationArgs);
|
|
53
|
+
if (result.error)
|
|
54
|
+
throw new ApiError(result);
|
|
55
|
+
return result.data ? result.data : result;
|
|
56
|
+
}
|
|
57
|
+
//@ts-expect-error TypeScript doesn't understand that we're creating an expected function
|
|
58
|
+
NewClass.prototype[propName] = wrapper;
|
|
59
|
+
});
|
|
60
|
+
// Return the new class
|
|
61
|
+
return NewClass;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Builds a type guard that reports whether a property name refers to a function
|
|
65
|
+
* on the given operations map, narrowing it to `keyof T`.
|
|
66
|
+
*
|
|
67
|
+
* @param baseType The operations map to test property names against.
|
|
68
|
+
* @returns A predicate that is `true` for keys whose value is a function.
|
|
69
|
+
*/
|
|
70
|
+
function createIsFunctionValidator(baseType) {
|
|
71
|
+
return (propName) => {
|
|
72
|
+
return typeof (baseType[propName]) == 'function';
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Abstract base for hey-api backed clients. Holds the immutable configuration
|
|
77
|
+
* and network client, and—when `debug` is enabled—installs request/response
|
|
78
|
+
* logging interceptors. Operation methods are added by {@link withOperations}.
|
|
79
|
+
*
|
|
80
|
+
* @typeParam C Configuration type, extending {@link ApiClientConfig}.
|
|
81
|
+
* @typeParam NC Network client type, extending {@link ApiClientNetworkClient}.
|
|
82
|
+
*/
|
|
83
|
+
class ApiClient {
|
|
84
|
+
/**
|
|
85
|
+
* Immutable configuration snapshot for this client instance.
|
|
86
|
+
* Accessible to subclasses only.
|
|
87
|
+
*/
|
|
88
|
+
_config;
|
|
89
|
+
/**
|
|
90
|
+
* The `@hey-api` network client bound to this instance.
|
|
91
|
+
* Injected into every operation call. Accessible to subclasses only.
|
|
92
|
+
*/
|
|
93
|
+
_client;
|
|
94
|
+
/** Whether debug logging is enabled for this client. */
|
|
95
|
+
get debug() {
|
|
96
|
+
return this._config.debug ?? false;
|
|
97
|
+
}
|
|
98
|
+
/** The configured client name, or `'API Client'` when unset. */
|
|
99
|
+
get name() {
|
|
100
|
+
return this._config.name ?? 'API Client';
|
|
101
|
+
}
|
|
102
|
+
/** The underlying network client used to perform operations. */
|
|
103
|
+
get client() {
|
|
104
|
+
return this._client;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @param config Immutable client configuration.
|
|
108
|
+
* @param client Network client used to perform operations; receives logging interceptors when `config.debug` is set.
|
|
109
|
+
*/
|
|
110
|
+
constructor(config, client) {
|
|
111
|
+
this._config = config;
|
|
112
|
+
this._client = client;
|
|
113
|
+
if (this._config.debug) {
|
|
114
|
+
const name = this._config.name ?? 'API Client';
|
|
115
|
+
this._client.interceptors.request.use(async (request) => {
|
|
116
|
+
console.log(`🔍 [${name}] Sending ${request.method} request to ${request.url}`);
|
|
117
|
+
return request;
|
|
118
|
+
});
|
|
119
|
+
this._client.interceptors.response.use((response, request) => {
|
|
120
|
+
console.log(`✨ [CMS API] Received response ${response.status} ${response.statusText} of type ${response.headers.get('Content-Type') ?? 'unknown'} for ${request.url}`);
|
|
121
|
+
return response;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Type guard for a hey-api error result (an object carrying `error`,
|
|
127
|
+
* `request` and `response`).
|
|
128
|
+
*
|
|
129
|
+
* @param toTest The value to inspect.
|
|
130
|
+
* @returns `true` when `toTest` is an error response.
|
|
131
|
+
*/
|
|
132
|
+
isErrorResponse(toTest) {
|
|
133
|
+
if (!this.isObject(toTest))
|
|
134
|
+
return false;
|
|
135
|
+
return toTest.error && toTest.request && toTest.response ? true : false;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Type guard for a hey-api success result (an object carrying `data`,
|
|
139
|
+
* `request` and `response`).
|
|
140
|
+
*
|
|
141
|
+
* @param toTest The value to inspect.
|
|
142
|
+
* @returns `true` when `toTest` is a data response.
|
|
143
|
+
*/
|
|
144
|
+
isDataResponse(toTest) {
|
|
145
|
+
if (!this.isObject(toTest))
|
|
146
|
+
return false;
|
|
147
|
+
return toTest.data && toTest.request && toTest.response ? true : false;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Type guard for a non-null object.
|
|
151
|
+
*
|
|
152
|
+
* @param toTest The value to inspect.
|
|
153
|
+
* @returns `true` when `toTest` is a non-null object.
|
|
154
|
+
*/
|
|
155
|
+
isObject(toTest) {
|
|
156
|
+
return typeof toTest === 'object' && toTest !== null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
exports.ApiClient = ApiClient;
|
|
160
|
+
/**
|
|
161
|
+
* Error thrown when an API operation returns an error result. Wraps the
|
|
162
|
+
* originating error payload together with the HTTP request and response for
|
|
163
|
+
* inspection.
|
|
164
|
+
*/
|
|
165
|
+
class ApiError extends Error {
|
|
166
|
+
/** Raw error context captured from the failed operation: the error payload and the originating HTTP request and response. */
|
|
167
|
+
_ctx;
|
|
168
|
+
/**
|
|
169
|
+
* @param data The operation error context: the error payload plus the HTTP request and response. A string error is used verbatim as the message; otherwise the message is derived from the response status.
|
|
170
|
+
*/
|
|
171
|
+
constructor(data) {
|
|
172
|
+
if (typeof data.error == 'string')
|
|
173
|
+
super(data.error);
|
|
174
|
+
else
|
|
175
|
+
super(`Optimizely CMS API Error: ${data.response?.status ?? 500} ${data.response?.statusText ?? 'Unknown error'}`);
|
|
176
|
+
this._ctx = data;
|
|
177
|
+
}
|
|
178
|
+
/** The error payload returned by the operation. */
|
|
179
|
+
get data() {
|
|
180
|
+
return this._ctx.error;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @deprecated Use {@link data} instead.
|
|
184
|
+
*/
|
|
185
|
+
get body() {
|
|
186
|
+
return this._ctx.error;
|
|
187
|
+
}
|
|
188
|
+
/** The HTTP request that produced the error. */
|
|
189
|
+
get request() {
|
|
190
|
+
return this._ctx.request;
|
|
191
|
+
}
|
|
192
|
+
/** The HTTP response that produced the error. */
|
|
193
|
+
get response() {
|
|
194
|
+
return this._ctx.response;
|
|
195
|
+
}
|
|
196
|
+
/** The HTTP status code of the response. */
|
|
197
|
+
get status() {
|
|
198
|
+
return this._ctx.response?.status ?? 500;
|
|
199
|
+
}
|
|
200
|
+
/** The HTTP status text of the response. */
|
|
201
|
+
get statusText() {
|
|
202
|
+
return this._ctx.response?.statusText ?? 'Unknown error';
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.ApiError = ApiError;
|
|
206
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AA8BA,wCAsCC;AAhED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,SAAgB,cAAc,CAAoE,IAAW,EAAE,UAAuB;IAEpI,oFAAoF;IACpF,aAAa;IACb,MAAM,QAAS,SAAQ,IAA6C;QAClE,YAAmB,GAAG,IAAW;YAC/B,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;QACjB,CAAC;KACF;IAED,2CAA2C;IAC3C,MAAM,mBAAmB,GAAG,yBAAyB,CAAC,UAAU,CAAC,CAAC;IAElE,sBAAsB;IACtB,CAAC;IAAC,MAAM,CAAC,mBAAmB,CAAC,UAAU,CAA8B,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QAEnH,KAAK,UAAU,OAAO,CAAkB,GAAG,IAAW;YAEpD,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;YAChC,aAAa,CAAC,CAAC,CAAC,GAAG;gBACjB,YAAY,EAAE,KAAK;gBACnB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DED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility function to extract all functions from a dictionary. This is typically used
|
|
3
|
+
* with the hey-api sdk.
|
|
4
|
+
*
|
|
5
|
+
* @typeparam Operations The hey-api SDK module shape (e.g. `typeof import('./client/sdk.gen')`).
|
|
6
|
+
*
|
|
7
|
+
* **Example:**
|
|
8
|
+
* ```
|
|
9
|
+
* import * as apiFunctions from './client/sdk.gen';
|
|
10
|
+
* type ApiOperations = OperationsList<typeof apiFunctions>;
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export type OperationsList<Operations extends Record<string, unknown> = Record<string, unknown>> = {
|
|
14
|
+
-readonly [KT in keyof Operations as Operations[KT] extends (...args: unknown[]) => unknown ? (KT extends string ? KT : never) : never]: Operations[KT];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Normalizes an operation's return type: a hey-api result
|
|
18
|
+
* (`Promise<{ data?, request, response }>`) is reduced to `Promise<TData>`;
|
|
19
|
+
* any other return type is left unchanged.
|
|
20
|
+
*/
|
|
21
|
+
type OperationReturnType<Op extends (...args: unknown[]) => unknown> = ReturnType<Op> extends Promise<{
|
|
22
|
+
data?: infer TData;
|
|
23
|
+
}> ? Promise<TData> : ReturnType<Op>;
|
|
24
|
+
/**
|
|
25
|
+
* Convert the extracted SDK functions to the normalized client
|
|
26
|
+
* functions that can be used.
|
|
27
|
+
*
|
|
28
|
+
* @typeparam L An {@link OperationsList} — the map of hey-api operation functions to convert.
|
|
29
|
+
*
|
|
30
|
+
* **Example:**
|
|
31
|
+
* ```
|
|
32
|
+
* import * as apiFunctions from './client/sdk.gen';
|
|
33
|
+
* type ApiOperations = OperationsList<typeof apiFunctions>;
|
|
34
|
+
* type ClientFunctions = ApiClientFunctions<ApiOperations>;
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export type ApiClientFunctions<L extends Record<string, (...args: unknown[]) => unknown>> = {
|
|
38
|
+
readonly [KT in keyof L]: (...args: Parameters<L[KT]>) => OperationReturnType<L[KT]>;
|
|
39
|
+
};
|
|
40
|
+
/** Any class (constructable) type. */
|
|
41
|
+
export type ClassDefinition = {
|
|
42
|
+
new (...args: any[]): any;
|
|
43
|
+
};
|
|
44
|
+
/** The keys of `T` whose values are not constructors. */
|
|
45
|
+
type NonConstructorKeys<T> = ({
|
|
46
|
+
[P in keyof T]: T[P] extends new () => object ? never : P;
|
|
47
|
+
})[keyof T];
|
|
48
|
+
/** `TBase` with its construct signature removed, leaving only static members. */
|
|
49
|
+
type OmitConstructor<TBase extends ClassDefinition> = Pick<TBase, NonConstructorKeys<TBase>>;
|
|
50
|
+
/**
|
|
51
|
+
* Produces the concrete derived-class type that results from applying a mixin
|
|
52
|
+
* to a base class. The constructor signature matches `TBase`; the instance type
|
|
53
|
+
* is `InstanceType<TBase> & Mixin`.
|
|
54
|
+
*
|
|
55
|
+
* @typeparam TBase The base class being extended (must be constructable).
|
|
56
|
+
* @typeparam Mixin The mixin object type whose members are merged into the instance.
|
|
57
|
+
*
|
|
58
|
+
* **Example:**
|
|
59
|
+
* ```typescript
|
|
60
|
+
* type CmsApiClientClass = ClassWithMixin<typeof ApiClient, ApiClientFunctions<CmsOperations>>;
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export type ClassWithMixin<TBase extends ClassDefinition, Mixin> = OmitConstructor<TBase> & {
|
|
64
|
+
new (...args: ConstructorParameters<TBase>): InstanceType<TBase> & Mixin;
|
|
65
|
+
};
|
|
66
|
+
/** Base configuration accepted by every {@link ApiClient}. */
|
|
67
|
+
export type ApiClientConfig = {
|
|
68
|
+
/** Enables request/response logging interceptors when `true`. */
|
|
69
|
+
debug?: boolean;
|
|
70
|
+
/** Optional display name used in log output; defaults to `'API Client'`. */
|
|
71
|
+
name?: string;
|
|
72
|
+
};
|
|
73
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@remkoj/hey-api-wrapper",
|
|
3
|
+
"displayName": "Wrap an Hey-API generated SDK into a single client object",
|
|
4
|
+
"description": "A compatibility package that wraps an Hey-API generated SDK into a single client object, providing a more convenient interface for users.",
|
|
5
|
+
"version": "6.0.0-rc.2",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/remkoj/optimizely-dxp-clients.git",
|
|
10
|
+
"directory": "packages/hey-api-wrapper"
|
|
11
|
+
},
|
|
12
|
+
"bugs": "https://github.com/remkoj/optimizely-dxp-clients/issues",
|
|
13
|
+
"homepage": "https://github.com/remkoj/optimizely-dxp-clients/blob/main/packages/hey-api-wrapper/README.md",
|
|
14
|
+
"type": "commonjs",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"./dist",
|
|
19
|
+
"./AGENTS.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"clean": "npx --yes rimraf -g ./dist ./*.tsbuildinfo",
|
|
23
|
+
"prepare": "tsc --build",
|
|
24
|
+
"watch": "tsc --watch",
|
|
25
|
+
"recompile": "yarn clean && tsc --build --force"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.9.3"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"tslib": "^2.8.1"
|
|
32
|
+
}
|
|
33
|
+
}
|