@geekmidas/client 9.0.2 → 10.0.0-alpha.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/dist/auth-fetcher-CBYuL6rx.d.cts +158 -0
- package/dist/auth-fetcher-CBYuL6rx.d.cts.map +1 -0
- package/dist/auth-fetcher-DOj0iz21.mjs +77 -0
- package/dist/auth-fetcher-DOj0iz21.mjs.map +1 -0
- package/dist/auth-fetcher-DxS8zOLc.d.mts +158 -0
- package/dist/auth-fetcher-DxS8zOLc.d.mts.map +1 -0
- package/dist/auth-fetcher-HILABmws.cjs +82 -0
- package/dist/auth-fetcher-HILABmws.cjs.map +1 -0
- package/dist/auth-fetcher.cjs +3 -76
- package/dist/auth-fetcher.d.cts +3 -158
- package/dist/auth-fetcher.d.mts +3 -158
- package/dist/auth-fetcher.mjs +3 -76
- package/dist/endpoint-hooks.d.mts +1 -1
- package/dist/fetcher-C1xtCKpT.d.mts +25 -0
- package/dist/fetcher-C1xtCKpT.d.mts.map +1 -0
- package/dist/fetcher-cDrfwN-Y.d.cts +25 -0
- package/dist/fetcher-cDrfwN-Y.d.cts.map +1 -0
- package/dist/fetcher.d.cts +3 -25
- package/dist/fetcher.d.mts +3 -25
- package/dist/index.cjs +8 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +6 -0
- package/dist/infer-6KKCgmg1.cjs +0 -0
- package/{src/infer.ts → dist/infer-DfRq5kIb.d.cts} +54 -83
- package/dist/infer-DfRq5kIb.d.cts.map +1 -0
- package/dist/infer-bTirNeNW.d.mts +133 -0
- package/dist/infer-bTirNeNW.d.mts.map +1 -0
- package/dist/infer-uCUq1whM.mjs +0 -0
- package/dist/infer.cjs +1 -0
- package/dist/infer.d.cts +2 -133
- package/dist/infer.d.mts +2 -133
- package/dist/infer.mjs +1 -0
- package/dist/openapi-hooks.cjs.map +1 -1
- package/dist/openapi-hooks.d.cts +3 -3
- package/dist/openapi-hooks.d.cts.map +1 -1
- package/dist/openapi-hooks.d.mts +1 -1
- package/dist/openapi-hooks.d.mts.map +1 -1
- package/dist/openapi-hooks.mjs.map +1 -1
- package/dist/react-query.d.cts +7 -7
- package/dist/react-query.d.mts +1 -1
- package/dist/types-BH8WgGrm.mjs +0 -0
- package/dist/{types-D8Jrl4o3.d.mts → types-BMGM6sZc.d.mts} +1 -1
- package/dist/{types-D8Jrl4o3.d.mts.map → types-BMGM6sZc.d.mts.map} +1 -1
- package/dist/types-a8gm_IaQ.cjs +0 -0
- package/dist/types.cjs +1 -0
- package/dist/types.d.mts +1 -1
- package/dist/types.mjs +1 -0
- package/package.json +8 -5
- package/CHANGELOG.md +0 -146
- package/dist/auth-fetcher.cjs.map +0 -1
- package/dist/auth-fetcher.d.cts.map +0 -1
- package/dist/auth-fetcher.d.mts.map +0 -1
- package/dist/auth-fetcher.mjs.map +0 -1
- package/dist/fetcher.d.cts.map +0 -1
- package/dist/fetcher.d.mts.map +0 -1
- package/dist/infer.d.cts.map +0 -1
- package/dist/infer.d.mts.map +0 -1
- package/src/__tests__/auth-fetcher.spec.ts +0 -505
- package/src/__tests__/endpoint-hooks.spec.tsx +0 -374
- package/src/__tests__/fetcher.spec.ts +0 -580
- package/src/__tests__/infer.integration.spec.ts +0 -188
- package/src/__tests__/infer.spec.ts +0 -189
- package/src/__tests__/method-restrictions.spec.tsx +0 -227
- package/src/__tests__/mutation-inference.spec.tsx +0 -343
- package/src/__tests__/openapi-hooks.spec.tsx +0 -557
- package/src/__tests__/react-query-infinite.spec.tsx +0 -1003
- package/src/__tests__/react-query-invalidation.spec.tsx +0 -239
- package/src/__tests__/react-query.spec.tsx +0 -582
- package/src/__tests__/setup.ts +0 -281
- package/src/__tests__/types.spec.ts +0 -134
- package/src/__tests__/url-parsing.spec.ts +0 -196
- package/src/auth-fetcher.ts +0 -288
- package/src/endpoint-hooks.ts +0 -346
- package/src/fetcher.ts +0 -171
- package/src/openapi-hooks.ts +0 -193
- package/src/openapi-types.d.ts +0 -440
- package/src/openapi.json +0 -595
- package/src/react-query.ts +0 -341
- package/src/types.ts +0 -226
- package/tsconfig.json +0 -12
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { FetcherOptions, TypedApiFunction } from "./types-DMQMHePI.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/auth-fetcher.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Security scheme object matching OpenAPI 3.1 specification.
|
|
7
|
+
*/
|
|
8
|
+
interface SecuritySchemeObject {
|
|
9
|
+
type: 'apiKey' | 'http' | 'mutualTLS' | 'oauth2' | 'openIdConnect';
|
|
10
|
+
description?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
in?: 'query' | 'header' | 'cookie';
|
|
13
|
+
scheme?: string;
|
|
14
|
+
bearerFormat?: string;
|
|
15
|
+
flows?: Record<string, unknown>;
|
|
16
|
+
openIdConnectUrl?: string;
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Extract all non-null security scheme IDs that are actually used in the API.
|
|
21
|
+
* This gives us the union of scheme names that endpoints require.
|
|
22
|
+
*/
|
|
23
|
+
type UsedSecuritySchemes<EndpointAuth extends Record<string, string | null>> = NonNullable<EndpointAuth[keyof EndpointAuth]>;
|
|
24
|
+
/**
|
|
25
|
+
* Interface for token storage and retrieval.
|
|
26
|
+
* Compatible with @geekmidas/auth TokenClient.
|
|
27
|
+
*/
|
|
28
|
+
interface TokenProvider {
|
|
29
|
+
/**
|
|
30
|
+
* Get a valid access token, refreshing if necessary.
|
|
31
|
+
*/
|
|
32
|
+
getValidAccessToken(): Promise<string | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Create Authorization headers from the current token.
|
|
35
|
+
*/
|
|
36
|
+
createValidAuthHeaders(): Promise<Record<string, string>>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Interface for API key providers.
|
|
40
|
+
*/
|
|
41
|
+
interface ApiKeyProvider {
|
|
42
|
+
/**
|
|
43
|
+
* Get the API key value.
|
|
44
|
+
*/
|
|
45
|
+
getApiKey(): Promise<string> | string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Interface for AWS SigV4 request signing.
|
|
49
|
+
*/
|
|
50
|
+
interface AwsSigner {
|
|
51
|
+
/**
|
|
52
|
+
* Sign a request with AWS SigV4.
|
|
53
|
+
* @param url - The request URL
|
|
54
|
+
* @param init - The request init object
|
|
55
|
+
* @returns Headers to add to the request
|
|
56
|
+
*/
|
|
57
|
+
sign(url: string, init: RequestInit): Promise<Record<string, string>>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Auth strategy configuration for a specific security scheme type.
|
|
61
|
+
*/
|
|
62
|
+
type AuthStrategy = {
|
|
63
|
+
type: 'bearer';
|
|
64
|
+
tokenProvider: TokenProvider;
|
|
65
|
+
} | {
|
|
66
|
+
type: 'apiKey';
|
|
67
|
+
apiKeyProvider: ApiKeyProvider;
|
|
68
|
+
headerName?: string;
|
|
69
|
+
} | {
|
|
70
|
+
type: 'iam';
|
|
71
|
+
signer: AwsSigner;
|
|
72
|
+
} | {
|
|
73
|
+
type: 'none';
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Options for creating an auth-aware fetcher.
|
|
77
|
+
*
|
|
78
|
+
* @template EndpointAuth - Map of endpoint strings to their auth scheme (or null for public)
|
|
79
|
+
* @template SecuritySchemes - Available security scheme definitions
|
|
80
|
+
*/
|
|
81
|
+
interface AuthFetcherOptions<EndpointAuth extends Record<string, string | null>, SecuritySchemes extends Record<string, SecuritySchemeObject>> extends Omit<FetcherOptions, 'onRequest'> {
|
|
82
|
+
/**
|
|
83
|
+
* Runtime map of endpoints to their required auth scheme.
|
|
84
|
+
* Generated by `gkm openapi --ts`.
|
|
85
|
+
*/
|
|
86
|
+
endpointAuth: EndpointAuth;
|
|
87
|
+
/**
|
|
88
|
+
* Security scheme definitions.
|
|
89
|
+
* Generated by `gkm openapi --ts`.
|
|
90
|
+
*/
|
|
91
|
+
securitySchemes: SecuritySchemes;
|
|
92
|
+
/**
|
|
93
|
+
* Auth strategies for security schemes that are actually used.
|
|
94
|
+
* Only schemes referenced in endpointAuth are required.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* // If endpointAuth has: { 'GET /users': 'jwt', 'POST /data': 'iam', 'GET /public': null }
|
|
99
|
+
* // Then authStrategies must include strategies for 'jwt' and 'iam'
|
|
100
|
+
* authStrategies: {
|
|
101
|
+
* jwt: { type: 'bearer', tokenProvider },
|
|
102
|
+
* iam: { type: 'iam', signer: awsSigner },
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
authStrategies: Record<UsedSecuritySchemes<EndpointAuth>, AuthStrategy>;
|
|
107
|
+
/**
|
|
108
|
+
* Optional request interceptor (runs after auth headers are added).
|
|
109
|
+
*/
|
|
110
|
+
onRequest?: (config: RequestInit) => RequestInit | Promise<RequestInit>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Creates an auth-aware fetcher that automatically applies the correct
|
|
114
|
+
* authentication based on the endpoint being called.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { endpointAuth, securitySchemes, paths } from './openapi';
|
|
119
|
+
* import { TokenClient } from '@geekmidas/auth/client';
|
|
120
|
+
*
|
|
121
|
+
* const tokenClient = new TokenClient({ ... });
|
|
122
|
+
*
|
|
123
|
+
* const api = createAuthAwareFetcher<paths>({
|
|
124
|
+
* baseURL: 'https://api.example.com',
|
|
125
|
+
* endpointAuth,
|
|
126
|
+
* securitySchemes,
|
|
127
|
+
* authStrategies: {
|
|
128
|
+
* bearer: { type: 'bearer', tokenProvider: tokenClient },
|
|
129
|
+
* iam: { type: 'iam', signer: awsSigner },
|
|
130
|
+
* },
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* // Bearer auth automatically applied
|
|
134
|
+
* const user = await api('GET /users/{id}', { params: { id: '123' } });
|
|
135
|
+
*
|
|
136
|
+
* // IAM SigV4 auth automatically applied
|
|
137
|
+
* const tenant = await api('POST /tenants', { body: { name: 'Acme' } });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
declare function createAuthAwareFetcher<Paths, EndpointAuth extends Record<string, string | null> = Record<string, string | null>, SecuritySchemes extends Record<string, SecuritySchemeObject> = Record<string, SecuritySchemeObject>>(options: AuthFetcherOptions<EndpointAuth, SecuritySchemes> & {
|
|
141
|
+
baseURL: string;
|
|
142
|
+
}): TypedApiFunction<Paths>;
|
|
143
|
+
/**
|
|
144
|
+
* Type helper to extract the security scheme ID from an endpoint.
|
|
145
|
+
*/
|
|
146
|
+
type GetEndpointAuth<EndpointAuth extends Record<string, string | null>, Endpoint extends keyof EndpointAuth> = EndpointAuth[Endpoint];
|
|
147
|
+
/**
|
|
148
|
+
* Type helper to get all authenticated endpoints.
|
|
149
|
+
*/
|
|
150
|
+
type AuthenticatedEndpoints<EndpointAuth extends Record<string, string | null>> = { [K in keyof EndpointAuth]: EndpointAuth[K] extends null ? never : K }[keyof EndpointAuth];
|
|
151
|
+
/**
|
|
152
|
+
* Type helper to get all public endpoints.
|
|
153
|
+
*/
|
|
154
|
+
type PublicEndpoints<EndpointAuth extends Record<string, string | null>> = { [K in keyof EndpointAuth]: EndpointAuth[K] extends null ? K : never }[keyof EndpointAuth];
|
|
155
|
+
//# sourceMappingURL=auth-fetcher.d.ts.map
|
|
156
|
+
//#endregion
|
|
157
|
+
export { ApiKeyProvider, AuthFetcherOptions, AuthStrategy, AuthenticatedEndpoints, AwsSigner, GetEndpointAuth, PublicEndpoints, SecuritySchemeObject, TokenProvider, UsedSecuritySchemes, createAuthAwareFetcher };
|
|
158
|
+
//# sourceMappingURL=auth-fetcher-CBYuL6rx.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-fetcher-CBYuL6rx.d.cts","names":[],"sources":["../src/auth-fetcher.ts"],"sourcesContent":[],"mappings":";;;;;AAcA;AAgBA;AAA+B,UAhBd,oBAAA,CAgBc;EAAA,IACT,EAAA,QAAA,GAAA,MAAA,GAAA,WAAA,GAAA,QAAA,GAAA,eAAA;EAAM,WACZ,CAAA,EAAA,MAAA;EAAY,IAAO,CAAA,EAAA,MAAA;EAAY,EAAA,CAA3C,EAAA,OAAA,GAAA,QAAA,GAAA,QAAA;EAAW,MAAA,CAAA,EAAA,MAAA;EAME,YAAA,CAAA,EAAA,MAAa;EAAA,KAAA,CAAA,EAjBrB,MAiBqB,CAAA,MAAA,EAAA,OAAA,CAAA;EAAA,gBAIN,CAAA,EAAA,MAAA;EAAO,CAAA,GAKI,EAAA,MAAA,CAAA,EAAA,OAAA;;AAAD;AAMlC;AAUA;;AAOyB,KAxCb,mBAwCa,CAAA,qBAvCH,MAuCG,CAAA,MAAA,EAAA,MAAA,GAAA,IAAA,CAAA,CAAA,GAtCrB,WAsCqB,CAtCT,YAsCS,CAAA,MAtCU,YAsCV,CAAA,CAAA;;;AAAqB;AAM9C;AAAwB,UAtCP,aAAA,CAsCO;EAAA;;;EAGW,mBAAA,EAAA,EArCX,OAqCW,CAAA,MAAA,GAAA,IAAA,CAAA;EASlB;;;EACW,sBACY,EAAA,EA3Cb,OA2Ca,CA3CL,MA2CK,CAAA,MAAA,EAAA,MAAA,CAAA,CAAA;;;;;AA4BI,UAjE3B,cAAA,CAiE2B;EAAY;;;EAAjC,SAKD,EAAA,EAlER,OAkEQ,CAAA,MAAA,CAAA,GAAA,MAAA;;;;;AAhCR,UA5BG,SAAA,CA4BH;EA+DE;;;;;;EAMe,IAE7B,CAAA,GAAA,EAAA,MAAA,EAAA,IAAA,EA5FuB,WA4FvB,CAAA,EA5FqC,OA4FrC,CA5F6C,MA4F7C,CAAA,MAAA,EAAA,MAAA,CAAA,CAAA;;;;;AAMkB,KA5FR,YAAA,GA4FQ;EAAK,IAAtB,EAAA,QAAA;EAAgB,aAAA,EA3FiB,aA2FjB;AAkGnB,CAAA,GAAY;EAAe,IAAA,EAAA,QAAA;EAAA,cACL,EA7Le,cA6Lf;EAAM,UACJ,CAAA,EAAA,MAAA;CAAY,GAAA;EACpB,IAAC,EAAA,KAAA;EAAQ,MAAA,EA9LC,SA8LD;AAKzB,CAAA,GAAY;EAAsB,IAAA,EAAA,MAAA;CAAA;;;;;;AAId;AAKR,UAnMK,kBAmMU,CAAA,qBAlML,MAkMK,CAAA,MAAA,EAAA,MAAA,GAAA,IAAA,CAAA,EAAA,wBAjMF,MAiME,CAAA,MAAA,EAjMa,oBAiMb,CAAA,CAAA,SAhMjB,IAgMiB,CAhMZ,cAgMY,EAAA,WAAA,CAAA,CAAA;EAAA;;;;EAGa,YAAC,EA9L1B,YA8L0B;EAAC;;AACtB;;mBAzLF;;;;;;;;;;;;;;;kBAgBD,OAAO,oBAAoB,eAAe;;;;uBAKrC,gBAAgB,cAAc,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+B5C,mDAEM,gCAAgC,uDAI7B,eAAe,wBAAwB,eAE9D,gCAGQ,mBAAmB,cAAc;;IAGxC,iBAAiB;;;;KAkGR,qCACU,sDACE,gBACpB,aAAa;;;;KAKL,4CACU,+CAET,eAAe,aAAa,0BAA0B,UAC3D;;;;KAKI,qCACU,+CAET,eAAe,aAAa,kBAAkB,kBACnD"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { TypedFetcher } from "./fetcher-CakjC9C7.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/auth-fetcher.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates an auth-aware fetcher that automatically applies the correct
|
|
6
|
+
* authentication based on the endpoint being called.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { endpointAuth, securitySchemes, paths } from './openapi';
|
|
11
|
+
* import { TokenClient } from '@geekmidas/auth/client';
|
|
12
|
+
*
|
|
13
|
+
* const tokenClient = new TokenClient({ ... });
|
|
14
|
+
*
|
|
15
|
+
* const api = createAuthAwareFetcher<paths>({
|
|
16
|
+
* baseURL: 'https://api.example.com',
|
|
17
|
+
* endpointAuth,
|
|
18
|
+
* securitySchemes,
|
|
19
|
+
* authStrategies: {
|
|
20
|
+
* bearer: { type: 'bearer', tokenProvider: tokenClient },
|
|
21
|
+
* iam: { type: 'iam', signer: awsSigner },
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Bearer auth automatically applied
|
|
26
|
+
* const user = await api('GET /users/{id}', { params: { id: '123' } });
|
|
27
|
+
*
|
|
28
|
+
* // IAM SigV4 auth automatically applied
|
|
29
|
+
* const tenant = await api('POST /tenants', { body: { name: 'Acme' } });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function createAuthAwareFetcher(options) {
|
|
33
|
+
const { endpointAuth, securitySchemes, authStrategies, onRequest: userOnRequest,...fetcherOptions } = options;
|
|
34
|
+
const baseFetcher = new TypedFetcher({
|
|
35
|
+
...fetcherOptions,
|
|
36
|
+
onRequest: userOnRequest
|
|
37
|
+
});
|
|
38
|
+
const fetcher = async (endpoint, config) => {
|
|
39
|
+
const schemeName = endpointAuth[endpoint];
|
|
40
|
+
let authHeaders = {};
|
|
41
|
+
if (schemeName) {
|
|
42
|
+
const scheme = securitySchemes[schemeName];
|
|
43
|
+
const strategy = authStrategies[schemeName];
|
|
44
|
+
if (strategy && scheme) authHeaders = await resolveAuthHeaders(strategy, scheme);
|
|
45
|
+
}
|
|
46
|
+
const existingHeaders = config && "headers" in config && config.headers ? config.headers : {};
|
|
47
|
+
const mergedConfig = {
|
|
48
|
+
...config,
|
|
49
|
+
headers: {
|
|
50
|
+
...authHeaders,
|
|
51
|
+
...existingHeaders
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
return baseFetcher.request(endpoint, mergedConfig);
|
|
55
|
+
};
|
|
56
|
+
return fetcher;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resolves auth headers based on the strategy and scheme.
|
|
60
|
+
*/
|
|
61
|
+
async function resolveAuthHeaders(strategy, scheme) {
|
|
62
|
+
switch (strategy.type) {
|
|
63
|
+
case "bearer": return strategy.tokenProvider.createValidAuthHeaders();
|
|
64
|
+
case "apiKey": {
|
|
65
|
+
const apiKey = await strategy.apiKeyProvider.getApiKey();
|
|
66
|
+
const headerName = strategy.headerName || scheme.name || "X-API-Key";
|
|
67
|
+
if (scheme.in === "header" || !scheme.in) return { [headerName]: apiKey };
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
case "iam": return {};
|
|
71
|
+
default: return {};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { createAuthAwareFetcher };
|
|
77
|
+
//# sourceMappingURL=auth-fetcher-DOj0iz21.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-fetcher-DOj0iz21.mjs","names":["options: AuthFetcherOptions<EndpointAuth, SecuritySchemes> & {\n\t\tbaseURL: string;\n\t}","endpoint: T","config?: FilteredRequestConfig<Paths, T>","authHeaders: Record<string, string>","strategy: AuthStrategy","scheme: SecuritySchemeObject"],"sources":["../src/auth-fetcher.ts"],"sourcesContent":["import { TypedFetcher } from './fetcher';\nimport type {\n\tExtractEndpointResponse,\n\tFetcherOptions,\n\tFilteredRequestConfig,\n\tTypedApiFunction,\n\tTypedEndpoint,\n} from './types';\n\nexport type { FetcherOptions } from './types';\n\n/**\n * Security scheme object matching OpenAPI 3.1 specification.\n */\nexport interface SecuritySchemeObject {\n\ttype: 'apiKey' | 'http' | 'mutualTLS' | 'oauth2' | 'openIdConnect';\n\tdescription?: string;\n\tname?: string;\n\tin?: 'query' | 'header' | 'cookie';\n\tscheme?: string;\n\tbearerFormat?: string;\n\tflows?: Record<string, unknown>;\n\topenIdConnectUrl?: string;\n\t[key: string]: unknown;\n}\n\n/**\n * Extract all non-null security scheme IDs that are actually used in the API.\n * This gives us the union of scheme names that endpoints require.\n */\nexport type UsedSecuritySchemes<\n\tEndpointAuth extends Record<string, string | null>,\n> = NonNullable<EndpointAuth[keyof EndpointAuth]>;\n\n/**\n * Interface for token storage and retrieval.\n * Compatible with @geekmidas/auth TokenClient.\n */\nexport interface TokenProvider {\n\t/**\n\t * Get a valid access token, refreshing if necessary.\n\t */\n\tgetValidAccessToken(): Promise<string | null>;\n\n\t/**\n\t * Create Authorization headers from the current token.\n\t */\n\tcreateValidAuthHeaders(): Promise<Record<string, string>>;\n}\n\n/**\n * Interface for API key providers.\n */\nexport interface ApiKeyProvider {\n\t/**\n\t * Get the API key value.\n\t */\n\tgetApiKey(): Promise<string> | string;\n}\n\n/**\n * Interface for AWS SigV4 request signing.\n */\nexport interface AwsSigner {\n\t/**\n\t * Sign a request with AWS SigV4.\n\t * @param url - The request URL\n\t * @param init - The request init object\n\t * @returns Headers to add to the request\n\t */\n\tsign(url: string, init: RequestInit): Promise<Record<string, string>>;\n}\n\n/**\n * Auth strategy configuration for a specific security scheme type.\n */\nexport type AuthStrategy =\n\t| { type: 'bearer'; tokenProvider: TokenProvider }\n\t| { type: 'apiKey'; apiKeyProvider: ApiKeyProvider; headerName?: string }\n\t| { type: 'iam'; signer: AwsSigner }\n\t| { type: 'none' };\n\n/**\n * Options for creating an auth-aware fetcher.\n *\n * @template EndpointAuth - Map of endpoint strings to their auth scheme (or null for public)\n * @template SecuritySchemes - Available security scheme definitions\n */\nexport interface AuthFetcherOptions<\n\tEndpointAuth extends Record<string, string | null>,\n\tSecuritySchemes extends Record<string, SecuritySchemeObject>,\n> extends Omit<FetcherOptions, 'onRequest'> {\n\t/**\n\t * Runtime map of endpoints to their required auth scheme.\n\t * Generated by `gkm openapi --ts`.\n\t */\n\tendpointAuth: EndpointAuth;\n\n\t/**\n\t * Security scheme definitions.\n\t * Generated by `gkm openapi --ts`.\n\t */\n\tsecuritySchemes: SecuritySchemes;\n\n\t/**\n\t * Auth strategies for security schemes that are actually used.\n\t * Only schemes referenced in endpointAuth are required.\n\t *\n\t * @example\n\t * ```typescript\n\t * // If endpointAuth has: { 'GET /users': 'jwt', 'POST /data': 'iam', 'GET /public': null }\n\t * // Then authStrategies must include strategies for 'jwt' and 'iam'\n\t * authStrategies: {\n\t * jwt: { type: 'bearer', tokenProvider },\n\t * iam: { type: 'iam', signer: awsSigner },\n\t * }\n\t * ```\n\t */\n\tauthStrategies: Record<UsedSecuritySchemes<EndpointAuth>, AuthStrategy>;\n\n\t/**\n\t * Optional request interceptor (runs after auth headers are added).\n\t */\n\tonRequest?: (config: RequestInit) => RequestInit | Promise<RequestInit>;\n}\n\n/**\n * Creates an auth-aware fetcher that automatically applies the correct\n * authentication based on the endpoint being called.\n *\n * @example\n * ```typescript\n * import { endpointAuth, securitySchemes, paths } from './openapi';\n * import { TokenClient } from '@geekmidas/auth/client';\n *\n * const tokenClient = new TokenClient({ ... });\n *\n * const api = createAuthAwareFetcher<paths>({\n * baseURL: 'https://api.example.com',\n * endpointAuth,\n * securitySchemes,\n * authStrategies: {\n * bearer: { type: 'bearer', tokenProvider: tokenClient },\n * iam: { type: 'iam', signer: awsSigner },\n * },\n * });\n *\n * // Bearer auth automatically applied\n * const user = await api('GET /users/{id}', { params: { id: '123' } });\n *\n * // IAM SigV4 auth automatically applied\n * const tenant = await api('POST /tenants', { body: { name: 'Acme' } });\n * ```\n */\nexport function createAuthAwareFetcher<\n\tPaths,\n\tEndpointAuth extends Record<string, string | null> = Record<\n\t\tstring,\n\t\tstring | null\n\t>,\n\tSecuritySchemes extends Record<string, SecuritySchemeObject> = Record<\n\t\tstring,\n\t\tSecuritySchemeObject\n\t>,\n>(\n\toptions: AuthFetcherOptions<EndpointAuth, SecuritySchemes> & {\n\t\tbaseURL: string;\n\t},\n): TypedApiFunction<Paths> {\n\tconst {\n\t\tendpointAuth,\n\t\tsecuritySchemes,\n\t\tauthStrategies,\n\t\tonRequest: userOnRequest,\n\t\t...fetcherOptions\n\t} = options;\n\n\t// Create base fetcher with user's onRequest if provided\n\tconst baseFetcher = new TypedFetcher<Paths>({\n\t\t...fetcherOptions,\n\t\tonRequest: userOnRequest,\n\t});\n\n\tconst fetcher = async <T extends TypedEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t): Promise<ExtractEndpointResponse<Paths, T>> => {\n\t\t// Look up auth requirement for this endpoint\n\t\tconst schemeName = endpointAuth[endpoint as keyof EndpointAuth] as\n\t\t\t| string\n\t\t\t| null;\n\n\t\tlet authHeaders: Record<string, string> = {};\n\n\t\tif (schemeName) {\n\t\t\tconst scheme = securitySchemes[schemeName as keyof SecuritySchemes];\n\t\t\t// Since authStrategies is now required to have all used schemes,\n\t\t\t// we can safely access it - TypeScript ensures the strategy exists\n\t\t\tconst strategy =\n\t\t\t\tauthStrategies[schemeName as UsedSecuritySchemes<EndpointAuth>];\n\n\t\t\tif (strategy && scheme) {\n\t\t\t\tauthHeaders = await resolveAuthHeaders(strategy, scheme);\n\t\t\t}\n\t\t}\n\n\t\t// Merge auth headers with config headers\n\t\tconst existingHeaders =\n\t\t\tconfig && 'headers' in config && config.headers\n\t\t\t\t? (config.headers as Record<string, string>)\n\t\t\t\t: {};\n\n\t\tconst mergedConfig = {\n\t\t\t...config,\n\t\t\theaders: {\n\t\t\t\t...authHeaders,\n\t\t\t\t...existingHeaders,\n\t\t\t},\n\t\t} as unknown as FilteredRequestConfig<Paths, T>;\n\n\t\treturn baseFetcher.request(endpoint, mergedConfig);\n\t};\n\n\treturn fetcher as TypedApiFunction<Paths>;\n}\n\n/**\n * Resolves auth headers based on the strategy and scheme.\n */\nasync function resolveAuthHeaders(\n\tstrategy: AuthStrategy,\n\tscheme: SecuritySchemeObject,\n): Promise<Record<string, string>> {\n\tswitch (strategy.type) {\n\t\tcase 'bearer': {\n\t\t\treturn strategy.tokenProvider.createValidAuthHeaders();\n\t\t}\n\n\t\tcase 'apiKey': {\n\t\t\tconst apiKey = await strategy.apiKeyProvider.getApiKey();\n\t\t\tconst headerName = strategy.headerName || scheme.name || 'X-API-Key';\n\n\t\t\tif (scheme.in === 'header' || !scheme.in) {\n\t\t\t\treturn { [headerName]: apiKey };\n\t\t\t}\n\t\t\t// Note: query and cookie API keys are handled differently\n\t\t\t// For now, we only support header-based API keys\n\t\t\treturn {};\n\t\t}\n\n\t\tcase 'iam': {\n\t\t\t// IAM signing requires the full URL and request config\n\t\t\t// This is a simplified version - full implementation would need\n\t\t\t// access to the complete request\n\t\t\t// For now, return empty - the actual signing should be done\n\t\t\t// in a custom onRequest interceptor if needed\n\t\t\treturn {};\n\t\t}\n\t\tdefault:\n\t\t\treturn {};\n\t}\n}\n\n/**\n * Type helper to extract the security scheme ID from an endpoint.\n */\nexport type GetEndpointAuth<\n\tEndpointAuth extends Record<string, string | null>,\n\tEndpoint extends keyof EndpointAuth,\n> = EndpointAuth[Endpoint];\n\n/**\n * Type helper to get all authenticated endpoints.\n */\nexport type AuthenticatedEndpoints<\n\tEndpointAuth extends Record<string, string | null>,\n> = {\n\t[K in keyof EndpointAuth]: EndpointAuth[K] extends null ? never : K;\n}[keyof EndpointAuth];\n\n/**\n * Type helper to get all public endpoints.\n */\nexport type PublicEndpoints<\n\tEndpointAuth extends Record<string, string | null>,\n> = {\n\t[K in keyof EndpointAuth]: EndpointAuth[K] extends null ? K : never;\n}[keyof EndpointAuth];\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0JA,SAAgB,uBAWfA,SAG0B;CAC1B,MAAM,EACL,cACA,iBACA,gBACA,WAAW,cACX,GAAG,gBACH,GAAG;CAGJ,MAAM,cAAc,IAAI,aAAoB;EAC3C,GAAG;EACH,WAAW;CACX;CAED,MAAM,UAAU,OACfC,UACAC,WACgD;EAEhD,MAAM,aAAa,aAAa;EAIhC,IAAIC,cAAsC,CAAE;AAE5C,MAAI,YAAY;GACf,MAAM,SAAS,gBAAgB;GAG/B,MAAM,WACL,eAAe;AAEhB,OAAI,YAAY,OACf,eAAc,MAAM,mBAAmB,UAAU,OAAO;EAEzD;EAGD,MAAM,kBACL,UAAU,aAAa,UAAU,OAAO,UACpC,OAAO,UACR,CAAE;EAEN,MAAM,eAAe;GACpB,GAAG;GACH,SAAS;IACR,GAAG;IACH,GAAG;GACH;EACD;AAED,SAAO,YAAY,QAAQ,UAAU,aAAa;CAClD;AAED,QAAO;AACP;;;;AAKD,eAAe,mBACdC,UACAC,QACkC;AAClC,SAAQ,SAAS,MAAjB;EACC,KAAK,SACJ,QAAO,SAAS,cAAc,wBAAwB;EAGvD,KAAK,UAAU;GACd,MAAM,SAAS,MAAM,SAAS,eAAe,WAAW;GACxD,MAAM,aAAa,SAAS,cAAc,OAAO,QAAQ;AAEzD,OAAI,OAAO,OAAO,aAAa,OAAO,GACrC,QAAO,GAAG,aAAa,OAAQ;AAIhC,UAAO,CAAE;EACT;EAED,KAAK,MAMJ,QAAO,CAAE;EAEV,QACC,QAAO,CAAE;CACV;AACD"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { FetcherOptions, TypedApiFunction } from "./types-BMGM6sZc.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/auth-fetcher.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Security scheme object matching OpenAPI 3.1 specification.
|
|
7
|
+
*/
|
|
8
|
+
interface SecuritySchemeObject {
|
|
9
|
+
type: 'apiKey' | 'http' | 'mutualTLS' | 'oauth2' | 'openIdConnect';
|
|
10
|
+
description?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
in?: 'query' | 'header' | 'cookie';
|
|
13
|
+
scheme?: string;
|
|
14
|
+
bearerFormat?: string;
|
|
15
|
+
flows?: Record<string, unknown>;
|
|
16
|
+
openIdConnectUrl?: string;
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Extract all non-null security scheme IDs that are actually used in the API.
|
|
21
|
+
* This gives us the union of scheme names that endpoints require.
|
|
22
|
+
*/
|
|
23
|
+
type UsedSecuritySchemes<EndpointAuth extends Record<string, string | null>> = NonNullable<EndpointAuth[keyof EndpointAuth]>;
|
|
24
|
+
/**
|
|
25
|
+
* Interface for token storage and retrieval.
|
|
26
|
+
* Compatible with @geekmidas/auth TokenClient.
|
|
27
|
+
*/
|
|
28
|
+
interface TokenProvider {
|
|
29
|
+
/**
|
|
30
|
+
* Get a valid access token, refreshing if necessary.
|
|
31
|
+
*/
|
|
32
|
+
getValidAccessToken(): Promise<string | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Create Authorization headers from the current token.
|
|
35
|
+
*/
|
|
36
|
+
createValidAuthHeaders(): Promise<Record<string, string>>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Interface for API key providers.
|
|
40
|
+
*/
|
|
41
|
+
interface ApiKeyProvider {
|
|
42
|
+
/**
|
|
43
|
+
* Get the API key value.
|
|
44
|
+
*/
|
|
45
|
+
getApiKey(): Promise<string> | string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Interface for AWS SigV4 request signing.
|
|
49
|
+
*/
|
|
50
|
+
interface AwsSigner {
|
|
51
|
+
/**
|
|
52
|
+
* Sign a request with AWS SigV4.
|
|
53
|
+
* @param url - The request URL
|
|
54
|
+
* @param init - The request init object
|
|
55
|
+
* @returns Headers to add to the request
|
|
56
|
+
*/
|
|
57
|
+
sign(url: string, init: RequestInit): Promise<Record<string, string>>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Auth strategy configuration for a specific security scheme type.
|
|
61
|
+
*/
|
|
62
|
+
type AuthStrategy = {
|
|
63
|
+
type: 'bearer';
|
|
64
|
+
tokenProvider: TokenProvider;
|
|
65
|
+
} | {
|
|
66
|
+
type: 'apiKey';
|
|
67
|
+
apiKeyProvider: ApiKeyProvider;
|
|
68
|
+
headerName?: string;
|
|
69
|
+
} | {
|
|
70
|
+
type: 'iam';
|
|
71
|
+
signer: AwsSigner;
|
|
72
|
+
} | {
|
|
73
|
+
type: 'none';
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Options for creating an auth-aware fetcher.
|
|
77
|
+
*
|
|
78
|
+
* @template EndpointAuth - Map of endpoint strings to their auth scheme (or null for public)
|
|
79
|
+
* @template SecuritySchemes - Available security scheme definitions
|
|
80
|
+
*/
|
|
81
|
+
interface AuthFetcherOptions<EndpointAuth extends Record<string, string | null>, SecuritySchemes extends Record<string, SecuritySchemeObject>> extends Omit<FetcherOptions, 'onRequest'> {
|
|
82
|
+
/**
|
|
83
|
+
* Runtime map of endpoints to their required auth scheme.
|
|
84
|
+
* Generated by `gkm openapi --ts`.
|
|
85
|
+
*/
|
|
86
|
+
endpointAuth: EndpointAuth;
|
|
87
|
+
/**
|
|
88
|
+
* Security scheme definitions.
|
|
89
|
+
* Generated by `gkm openapi --ts`.
|
|
90
|
+
*/
|
|
91
|
+
securitySchemes: SecuritySchemes;
|
|
92
|
+
/**
|
|
93
|
+
* Auth strategies for security schemes that are actually used.
|
|
94
|
+
* Only schemes referenced in endpointAuth are required.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* // If endpointAuth has: { 'GET /users': 'jwt', 'POST /data': 'iam', 'GET /public': null }
|
|
99
|
+
* // Then authStrategies must include strategies for 'jwt' and 'iam'
|
|
100
|
+
* authStrategies: {
|
|
101
|
+
* jwt: { type: 'bearer', tokenProvider },
|
|
102
|
+
* iam: { type: 'iam', signer: awsSigner },
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
authStrategies: Record<UsedSecuritySchemes<EndpointAuth>, AuthStrategy>;
|
|
107
|
+
/**
|
|
108
|
+
* Optional request interceptor (runs after auth headers are added).
|
|
109
|
+
*/
|
|
110
|
+
onRequest?: (config: RequestInit) => RequestInit | Promise<RequestInit>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Creates an auth-aware fetcher that automatically applies the correct
|
|
114
|
+
* authentication based on the endpoint being called.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { endpointAuth, securitySchemes, paths } from './openapi';
|
|
119
|
+
* import { TokenClient } from '@geekmidas/auth/client';
|
|
120
|
+
*
|
|
121
|
+
* const tokenClient = new TokenClient({ ... });
|
|
122
|
+
*
|
|
123
|
+
* const api = createAuthAwareFetcher<paths>({
|
|
124
|
+
* baseURL: 'https://api.example.com',
|
|
125
|
+
* endpointAuth,
|
|
126
|
+
* securitySchemes,
|
|
127
|
+
* authStrategies: {
|
|
128
|
+
* bearer: { type: 'bearer', tokenProvider: tokenClient },
|
|
129
|
+
* iam: { type: 'iam', signer: awsSigner },
|
|
130
|
+
* },
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* // Bearer auth automatically applied
|
|
134
|
+
* const user = await api('GET /users/{id}', { params: { id: '123' } });
|
|
135
|
+
*
|
|
136
|
+
* // IAM SigV4 auth automatically applied
|
|
137
|
+
* const tenant = await api('POST /tenants', { body: { name: 'Acme' } });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
declare function createAuthAwareFetcher<Paths, EndpointAuth extends Record<string, string | null> = Record<string, string | null>, SecuritySchemes extends Record<string, SecuritySchemeObject> = Record<string, SecuritySchemeObject>>(options: AuthFetcherOptions<EndpointAuth, SecuritySchemes> & {
|
|
141
|
+
baseURL: string;
|
|
142
|
+
}): TypedApiFunction<Paths>;
|
|
143
|
+
/**
|
|
144
|
+
* Type helper to extract the security scheme ID from an endpoint.
|
|
145
|
+
*/
|
|
146
|
+
type GetEndpointAuth<EndpointAuth extends Record<string, string | null>, Endpoint extends keyof EndpointAuth> = EndpointAuth[Endpoint];
|
|
147
|
+
/**
|
|
148
|
+
* Type helper to get all authenticated endpoints.
|
|
149
|
+
*/
|
|
150
|
+
type AuthenticatedEndpoints<EndpointAuth extends Record<string, string | null>> = { [K in keyof EndpointAuth]: EndpointAuth[K] extends null ? never : K }[keyof EndpointAuth];
|
|
151
|
+
/**
|
|
152
|
+
* Type helper to get all public endpoints.
|
|
153
|
+
*/
|
|
154
|
+
type PublicEndpoints<EndpointAuth extends Record<string, string | null>> = { [K in keyof EndpointAuth]: EndpointAuth[K] extends null ? K : never }[keyof EndpointAuth];
|
|
155
|
+
//# sourceMappingURL=auth-fetcher.d.ts.map
|
|
156
|
+
//#endregion
|
|
157
|
+
export { ApiKeyProvider, AuthFetcherOptions, AuthStrategy, AuthenticatedEndpoints, AwsSigner, GetEndpointAuth, PublicEndpoints, SecuritySchemeObject, TokenProvider, UsedSecuritySchemes, createAuthAwareFetcher };
|
|
158
|
+
//# sourceMappingURL=auth-fetcher-DxS8zOLc.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-fetcher-DxS8zOLc.d.mts","names":[],"sources":["../src/auth-fetcher.ts"],"sourcesContent":[],"mappings":";;;;;AAcA;AAgBA;AAA+B,UAhBd,oBAAA,CAgBc;EAAA,IACT,EAAA,QAAA,GAAA,MAAA,GAAA,WAAA,GAAA,QAAA,GAAA,eAAA;EAAM,WACZ,CAAA,EAAA,MAAA;EAAY,IAAO,CAAA,EAAA,MAAA;EAAY,EAAA,CAA3C,EAAA,OAAA,GAAA,QAAA,GAAA,QAAA;EAAW,MAAA,CAAA,EAAA,MAAA;EAME,YAAA,CAAA,EAAA,MAAa;EAAA,KAAA,CAAA,EAjBrB,MAiBqB,CAAA,MAAA,EAAA,OAAA,CAAA;EAAA,gBAIN,CAAA,EAAA,MAAA;EAAO,CAAA,GAKI,EAAA,MAAA,CAAA,EAAA,OAAA;;AAAD;AAMlC;AAUA;;AAOyB,KAxCb,mBAwCa,CAAA,qBAvCH,MAuCG,CAAA,MAAA,EAAA,MAAA,GAAA,IAAA,CAAA,CAAA,GAtCrB,WAsCqB,CAtCT,YAsCS,CAAA,MAtCU,YAsCV,CAAA,CAAA;;;AAAqB;AAM9C;AAAwB,UAtCP,aAAA,CAsCO;EAAA;;;EAGW,mBAAA,EAAA,EArCX,OAqCW,CAAA,MAAA,GAAA,IAAA,CAAA;EASlB;;;EACW,sBACY,EAAA,EA3Cb,OA2Ca,CA3CL,MA2CK,CAAA,MAAA,EAAA,MAAA,CAAA,CAAA;;;;;AA4BI,UAjE3B,cAAA,CAiE2B;EAAY;;;EAAjC,SAKD,EAAA,EAlER,OAkEQ,CAAA,MAAA,CAAA,GAAA,MAAA;;;;;AAhCR,UA5BG,SAAA,CA4BH;EA+DE;;;;;;EAMe,IAE7B,CAAA,GAAA,EAAA,MAAA,EAAA,IAAA,EA5FuB,WA4FvB,CAAA,EA5FqC,OA4FrC,CA5F6C,MA4F7C,CAAA,MAAA,EAAA,MAAA,CAAA,CAAA;;;;;AAMkB,KA5FR,YAAA,GA4FQ;EAAK,IAAtB,EAAA,QAAA;EAAgB,aAAA,EA3FiB,aA2FjB;AAkGnB,CAAA,GAAY;EAAe,IAAA,EAAA,QAAA;EAAA,cACL,EA7Le,cA6Lf;EAAM,UACJ,CAAA,EAAA,MAAA;CAAY,GAAA;EACpB,IAAC,EAAA,KAAA;EAAQ,MAAA,EA9LC,SA8LD;AAKzB,CAAA,GAAY;EAAsB,IAAA,EAAA,MAAA;CAAA;;;;;;AAId;AAKR,UAnMK,kBAmMU,CAAA,qBAlML,MAkMK,CAAA,MAAA,EAAA,MAAA,GAAA,IAAA,CAAA,EAAA,wBAjMF,MAiME,CAAA,MAAA,EAjMa,oBAiMb,CAAA,CAAA,SAhMjB,IAgMiB,CAhMZ,cAgMY,EAAA,WAAA,CAAA,CAAA;EAAA;;;;EAGa,YAAC,EA9L1B,YA8L0B;EAAC;;AACtB;;mBAzLF;;;;;;;;;;;;;;;kBAgBD,OAAO,oBAAoB,eAAe;;;;uBAKrC,gBAAgB,cAAc,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+B5C,mDAEM,gCAAgC,uDAI7B,eAAe,wBAAwB,eAE9D,gCAGQ,mBAAmB,cAAc;;IAGxC,iBAAiB;;;;KAkGR,qCACU,sDACE,gBACpB,aAAa;;;;KAKL,4CACU,+CAET,eAAe,aAAa,0BAA0B,UAC3D;;;;KAKI,qCACU,+CAET,eAAe,aAAa,kBAAkB,kBACnD"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const require_fetcher = require('./fetcher-CwnSqc4D.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/auth-fetcher.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates an auth-aware fetcher that automatically applies the correct
|
|
6
|
+
* authentication based on the endpoint being called.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { endpointAuth, securitySchemes, paths } from './openapi';
|
|
11
|
+
* import { TokenClient } from '@geekmidas/auth/client';
|
|
12
|
+
*
|
|
13
|
+
* const tokenClient = new TokenClient({ ... });
|
|
14
|
+
*
|
|
15
|
+
* const api = createAuthAwareFetcher<paths>({
|
|
16
|
+
* baseURL: 'https://api.example.com',
|
|
17
|
+
* endpointAuth,
|
|
18
|
+
* securitySchemes,
|
|
19
|
+
* authStrategies: {
|
|
20
|
+
* bearer: { type: 'bearer', tokenProvider: tokenClient },
|
|
21
|
+
* iam: { type: 'iam', signer: awsSigner },
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Bearer auth automatically applied
|
|
26
|
+
* const user = await api('GET /users/{id}', { params: { id: '123' } });
|
|
27
|
+
*
|
|
28
|
+
* // IAM SigV4 auth automatically applied
|
|
29
|
+
* const tenant = await api('POST /tenants', { body: { name: 'Acme' } });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function createAuthAwareFetcher(options) {
|
|
33
|
+
const { endpointAuth, securitySchemes, authStrategies, onRequest: userOnRequest,...fetcherOptions } = options;
|
|
34
|
+
const baseFetcher = new require_fetcher.TypedFetcher({
|
|
35
|
+
...fetcherOptions,
|
|
36
|
+
onRequest: userOnRequest
|
|
37
|
+
});
|
|
38
|
+
const fetcher = async (endpoint, config) => {
|
|
39
|
+
const schemeName = endpointAuth[endpoint];
|
|
40
|
+
let authHeaders = {};
|
|
41
|
+
if (schemeName) {
|
|
42
|
+
const scheme = securitySchemes[schemeName];
|
|
43
|
+
const strategy = authStrategies[schemeName];
|
|
44
|
+
if (strategy && scheme) authHeaders = await resolveAuthHeaders(strategy, scheme);
|
|
45
|
+
}
|
|
46
|
+
const existingHeaders = config && "headers" in config && config.headers ? config.headers : {};
|
|
47
|
+
const mergedConfig = {
|
|
48
|
+
...config,
|
|
49
|
+
headers: {
|
|
50
|
+
...authHeaders,
|
|
51
|
+
...existingHeaders
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
return baseFetcher.request(endpoint, mergedConfig);
|
|
55
|
+
};
|
|
56
|
+
return fetcher;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resolves auth headers based on the strategy and scheme.
|
|
60
|
+
*/
|
|
61
|
+
async function resolveAuthHeaders(strategy, scheme) {
|
|
62
|
+
switch (strategy.type) {
|
|
63
|
+
case "bearer": return strategy.tokenProvider.createValidAuthHeaders();
|
|
64
|
+
case "apiKey": {
|
|
65
|
+
const apiKey = await strategy.apiKeyProvider.getApiKey();
|
|
66
|
+
const headerName = strategy.headerName || scheme.name || "X-API-Key";
|
|
67
|
+
if (scheme.in === "header" || !scheme.in) return { [headerName]: apiKey };
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
case "iam": return {};
|
|
71
|
+
default: return {};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
Object.defineProperty(exports, 'createAuthAwareFetcher', {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: function () {
|
|
79
|
+
return createAuthAwareFetcher;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
//# sourceMappingURL=auth-fetcher-HILABmws.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-fetcher-HILABmws.cjs","names":["options: AuthFetcherOptions<EndpointAuth, SecuritySchemes> & {\n\t\tbaseURL: string;\n\t}","TypedFetcher","endpoint: T","config?: FilteredRequestConfig<Paths, T>","authHeaders: Record<string, string>","strategy: AuthStrategy","scheme: SecuritySchemeObject"],"sources":["../src/auth-fetcher.ts"],"sourcesContent":["import { TypedFetcher } from './fetcher';\nimport type {\n\tExtractEndpointResponse,\n\tFetcherOptions,\n\tFilteredRequestConfig,\n\tTypedApiFunction,\n\tTypedEndpoint,\n} from './types';\n\nexport type { FetcherOptions } from './types';\n\n/**\n * Security scheme object matching OpenAPI 3.1 specification.\n */\nexport interface SecuritySchemeObject {\n\ttype: 'apiKey' | 'http' | 'mutualTLS' | 'oauth2' | 'openIdConnect';\n\tdescription?: string;\n\tname?: string;\n\tin?: 'query' | 'header' | 'cookie';\n\tscheme?: string;\n\tbearerFormat?: string;\n\tflows?: Record<string, unknown>;\n\topenIdConnectUrl?: string;\n\t[key: string]: unknown;\n}\n\n/**\n * Extract all non-null security scheme IDs that are actually used in the API.\n * This gives us the union of scheme names that endpoints require.\n */\nexport type UsedSecuritySchemes<\n\tEndpointAuth extends Record<string, string | null>,\n> = NonNullable<EndpointAuth[keyof EndpointAuth]>;\n\n/**\n * Interface for token storage and retrieval.\n * Compatible with @geekmidas/auth TokenClient.\n */\nexport interface TokenProvider {\n\t/**\n\t * Get a valid access token, refreshing if necessary.\n\t */\n\tgetValidAccessToken(): Promise<string | null>;\n\n\t/**\n\t * Create Authorization headers from the current token.\n\t */\n\tcreateValidAuthHeaders(): Promise<Record<string, string>>;\n}\n\n/**\n * Interface for API key providers.\n */\nexport interface ApiKeyProvider {\n\t/**\n\t * Get the API key value.\n\t */\n\tgetApiKey(): Promise<string> | string;\n}\n\n/**\n * Interface for AWS SigV4 request signing.\n */\nexport interface AwsSigner {\n\t/**\n\t * Sign a request with AWS SigV4.\n\t * @param url - The request URL\n\t * @param init - The request init object\n\t * @returns Headers to add to the request\n\t */\n\tsign(url: string, init: RequestInit): Promise<Record<string, string>>;\n}\n\n/**\n * Auth strategy configuration for a specific security scheme type.\n */\nexport type AuthStrategy =\n\t| { type: 'bearer'; tokenProvider: TokenProvider }\n\t| { type: 'apiKey'; apiKeyProvider: ApiKeyProvider; headerName?: string }\n\t| { type: 'iam'; signer: AwsSigner }\n\t| { type: 'none' };\n\n/**\n * Options for creating an auth-aware fetcher.\n *\n * @template EndpointAuth - Map of endpoint strings to their auth scheme (or null for public)\n * @template SecuritySchemes - Available security scheme definitions\n */\nexport interface AuthFetcherOptions<\n\tEndpointAuth extends Record<string, string | null>,\n\tSecuritySchemes extends Record<string, SecuritySchemeObject>,\n> extends Omit<FetcherOptions, 'onRequest'> {\n\t/**\n\t * Runtime map of endpoints to their required auth scheme.\n\t * Generated by `gkm openapi --ts`.\n\t */\n\tendpointAuth: EndpointAuth;\n\n\t/**\n\t * Security scheme definitions.\n\t * Generated by `gkm openapi --ts`.\n\t */\n\tsecuritySchemes: SecuritySchemes;\n\n\t/**\n\t * Auth strategies for security schemes that are actually used.\n\t * Only schemes referenced in endpointAuth are required.\n\t *\n\t * @example\n\t * ```typescript\n\t * // If endpointAuth has: { 'GET /users': 'jwt', 'POST /data': 'iam', 'GET /public': null }\n\t * // Then authStrategies must include strategies for 'jwt' and 'iam'\n\t * authStrategies: {\n\t * jwt: { type: 'bearer', tokenProvider },\n\t * iam: { type: 'iam', signer: awsSigner },\n\t * }\n\t * ```\n\t */\n\tauthStrategies: Record<UsedSecuritySchemes<EndpointAuth>, AuthStrategy>;\n\n\t/**\n\t * Optional request interceptor (runs after auth headers are added).\n\t */\n\tonRequest?: (config: RequestInit) => RequestInit | Promise<RequestInit>;\n}\n\n/**\n * Creates an auth-aware fetcher that automatically applies the correct\n * authentication based on the endpoint being called.\n *\n * @example\n * ```typescript\n * import { endpointAuth, securitySchemes, paths } from './openapi';\n * import { TokenClient } from '@geekmidas/auth/client';\n *\n * const tokenClient = new TokenClient({ ... });\n *\n * const api = createAuthAwareFetcher<paths>({\n * baseURL: 'https://api.example.com',\n * endpointAuth,\n * securitySchemes,\n * authStrategies: {\n * bearer: { type: 'bearer', tokenProvider: tokenClient },\n * iam: { type: 'iam', signer: awsSigner },\n * },\n * });\n *\n * // Bearer auth automatically applied\n * const user = await api('GET /users/{id}', { params: { id: '123' } });\n *\n * // IAM SigV4 auth automatically applied\n * const tenant = await api('POST /tenants', { body: { name: 'Acme' } });\n * ```\n */\nexport function createAuthAwareFetcher<\n\tPaths,\n\tEndpointAuth extends Record<string, string | null> = Record<\n\t\tstring,\n\t\tstring | null\n\t>,\n\tSecuritySchemes extends Record<string, SecuritySchemeObject> = Record<\n\t\tstring,\n\t\tSecuritySchemeObject\n\t>,\n>(\n\toptions: AuthFetcherOptions<EndpointAuth, SecuritySchemes> & {\n\t\tbaseURL: string;\n\t},\n): TypedApiFunction<Paths> {\n\tconst {\n\t\tendpointAuth,\n\t\tsecuritySchemes,\n\t\tauthStrategies,\n\t\tonRequest: userOnRequest,\n\t\t...fetcherOptions\n\t} = options;\n\n\t// Create base fetcher with user's onRequest if provided\n\tconst baseFetcher = new TypedFetcher<Paths>({\n\t\t...fetcherOptions,\n\t\tonRequest: userOnRequest,\n\t});\n\n\tconst fetcher = async <T extends TypedEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t): Promise<ExtractEndpointResponse<Paths, T>> => {\n\t\t// Look up auth requirement for this endpoint\n\t\tconst schemeName = endpointAuth[endpoint as keyof EndpointAuth] as\n\t\t\t| string\n\t\t\t| null;\n\n\t\tlet authHeaders: Record<string, string> = {};\n\n\t\tif (schemeName) {\n\t\t\tconst scheme = securitySchemes[schemeName as keyof SecuritySchemes];\n\t\t\t// Since authStrategies is now required to have all used schemes,\n\t\t\t// we can safely access it - TypeScript ensures the strategy exists\n\t\t\tconst strategy =\n\t\t\t\tauthStrategies[schemeName as UsedSecuritySchemes<EndpointAuth>];\n\n\t\t\tif (strategy && scheme) {\n\t\t\t\tauthHeaders = await resolveAuthHeaders(strategy, scheme);\n\t\t\t}\n\t\t}\n\n\t\t// Merge auth headers with config headers\n\t\tconst existingHeaders =\n\t\t\tconfig && 'headers' in config && config.headers\n\t\t\t\t? (config.headers as Record<string, string>)\n\t\t\t\t: {};\n\n\t\tconst mergedConfig = {\n\t\t\t...config,\n\t\t\theaders: {\n\t\t\t\t...authHeaders,\n\t\t\t\t...existingHeaders,\n\t\t\t},\n\t\t} as unknown as FilteredRequestConfig<Paths, T>;\n\n\t\treturn baseFetcher.request(endpoint, mergedConfig);\n\t};\n\n\treturn fetcher as TypedApiFunction<Paths>;\n}\n\n/**\n * Resolves auth headers based on the strategy and scheme.\n */\nasync function resolveAuthHeaders(\n\tstrategy: AuthStrategy,\n\tscheme: SecuritySchemeObject,\n): Promise<Record<string, string>> {\n\tswitch (strategy.type) {\n\t\tcase 'bearer': {\n\t\t\treturn strategy.tokenProvider.createValidAuthHeaders();\n\t\t}\n\n\t\tcase 'apiKey': {\n\t\t\tconst apiKey = await strategy.apiKeyProvider.getApiKey();\n\t\t\tconst headerName = strategy.headerName || scheme.name || 'X-API-Key';\n\n\t\t\tif (scheme.in === 'header' || !scheme.in) {\n\t\t\t\treturn { [headerName]: apiKey };\n\t\t\t}\n\t\t\t// Note: query and cookie API keys are handled differently\n\t\t\t// For now, we only support header-based API keys\n\t\t\treturn {};\n\t\t}\n\n\t\tcase 'iam': {\n\t\t\t// IAM signing requires the full URL and request config\n\t\t\t// This is a simplified version - full implementation would need\n\t\t\t// access to the complete request\n\t\t\t// For now, return empty - the actual signing should be done\n\t\t\t// in a custom onRequest interceptor if needed\n\t\t\treturn {};\n\t\t}\n\t\tdefault:\n\t\t\treturn {};\n\t}\n}\n\n/**\n * Type helper to extract the security scheme ID from an endpoint.\n */\nexport type GetEndpointAuth<\n\tEndpointAuth extends Record<string, string | null>,\n\tEndpoint extends keyof EndpointAuth,\n> = EndpointAuth[Endpoint];\n\n/**\n * Type helper to get all authenticated endpoints.\n */\nexport type AuthenticatedEndpoints<\n\tEndpointAuth extends Record<string, string | null>,\n> = {\n\t[K in keyof EndpointAuth]: EndpointAuth[K] extends null ? never : K;\n}[keyof EndpointAuth];\n\n/**\n * Type helper to get all public endpoints.\n */\nexport type PublicEndpoints<\n\tEndpointAuth extends Record<string, string | null>,\n> = {\n\t[K in keyof EndpointAuth]: EndpointAuth[K] extends null ? K : never;\n}[keyof EndpointAuth];\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0JA,SAAgB,uBAWfA,SAG0B;CAC1B,MAAM,EACL,cACA,iBACA,gBACA,WAAW,cACX,GAAG,gBACH,GAAG;CAGJ,MAAM,cAAc,IAAIC,6BAAoB;EAC3C,GAAG;EACH,WAAW;CACX;CAED,MAAM,UAAU,OACfC,UACAC,WACgD;EAEhD,MAAM,aAAa,aAAa;EAIhC,IAAIC,cAAsC,CAAE;AAE5C,MAAI,YAAY;GACf,MAAM,SAAS,gBAAgB;GAG/B,MAAM,WACL,eAAe;AAEhB,OAAI,YAAY,OACf,eAAc,MAAM,mBAAmB,UAAU,OAAO;EAEzD;EAGD,MAAM,kBACL,UAAU,aAAa,UAAU,OAAO,UACpC,OAAO,UACR,CAAE;EAEN,MAAM,eAAe;GACpB,GAAG;GACH,SAAS;IACR,GAAG;IACH,GAAG;GACH;EACD;AAED,SAAO,YAAY,QAAQ,UAAU,aAAa;CAClD;AAED,QAAO;AACP;;;;AAKD,eAAe,mBACdC,UACAC,QACkC;AAClC,SAAQ,SAAS,MAAjB;EACC,KAAK,SACJ,QAAO,SAAS,cAAc,wBAAwB;EAGvD,KAAK,UAAU;GACd,MAAM,SAAS,MAAM,SAAS,eAAe,WAAW;GACxD,MAAM,aAAa,SAAS,cAAc,OAAO,QAAQ;AAEzD,OAAI,OAAO,OAAO,aAAa,OAAO,GACrC,QAAO,GAAG,aAAa,OAAQ;AAIhC,UAAO,CAAE;EACT;EAED,KAAK,MAMJ,QAAO,CAAE;EAEV,QACC,QAAO,CAAE;CACV;AACD"}
|
package/dist/auth-fetcher.cjs
CHANGED
|
@@ -1,77 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require('./fetcher-CwnSqc4D.cjs');
|
|
2
|
+
const require_auth_fetcher = require('./auth-fetcher-HILABmws.cjs');
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates an auth-aware fetcher that automatically applies the correct
|
|
6
|
-
* authentication based on the endpoint being called.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import { endpointAuth, securitySchemes, paths } from './openapi';
|
|
11
|
-
* import { TokenClient } from '@geekmidas/auth/client';
|
|
12
|
-
*
|
|
13
|
-
* const tokenClient = new TokenClient({ ... });
|
|
14
|
-
*
|
|
15
|
-
* const api = createAuthAwareFetcher<paths>({
|
|
16
|
-
* baseURL: 'https://api.example.com',
|
|
17
|
-
* endpointAuth,
|
|
18
|
-
* securitySchemes,
|
|
19
|
-
* authStrategies: {
|
|
20
|
-
* bearer: { type: 'bearer', tokenProvider: tokenClient },
|
|
21
|
-
* iam: { type: 'iam', signer: awsSigner },
|
|
22
|
-
* },
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* // Bearer auth automatically applied
|
|
26
|
-
* const user = await api('GET /users/{id}', { params: { id: '123' } });
|
|
27
|
-
*
|
|
28
|
-
* // IAM SigV4 auth automatically applied
|
|
29
|
-
* const tenant = await api('POST /tenants', { body: { name: 'Acme' } });
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
function createAuthAwareFetcher(options) {
|
|
33
|
-
const { endpointAuth, securitySchemes, authStrategies, onRequest: userOnRequest,...fetcherOptions } = options;
|
|
34
|
-
const baseFetcher = new require_fetcher.TypedFetcher({
|
|
35
|
-
...fetcherOptions,
|
|
36
|
-
onRequest: userOnRequest
|
|
37
|
-
});
|
|
38
|
-
const fetcher = async (endpoint, config) => {
|
|
39
|
-
const schemeName = endpointAuth[endpoint];
|
|
40
|
-
let authHeaders = {};
|
|
41
|
-
if (schemeName) {
|
|
42
|
-
const scheme = securitySchemes[schemeName];
|
|
43
|
-
const strategy = authStrategies[schemeName];
|
|
44
|
-
if (strategy && scheme) authHeaders = await resolveAuthHeaders(strategy, scheme);
|
|
45
|
-
}
|
|
46
|
-
const existingHeaders = config && "headers" in config && config.headers ? config.headers : {};
|
|
47
|
-
const mergedConfig = {
|
|
48
|
-
...config,
|
|
49
|
-
headers: {
|
|
50
|
-
...authHeaders,
|
|
51
|
-
...existingHeaders
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
return baseFetcher.request(endpoint, mergedConfig);
|
|
55
|
-
};
|
|
56
|
-
return fetcher;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Resolves auth headers based on the strategy and scheme.
|
|
60
|
-
*/
|
|
61
|
-
async function resolveAuthHeaders(strategy, scheme) {
|
|
62
|
-
switch (strategy.type) {
|
|
63
|
-
case "bearer": return strategy.tokenProvider.createValidAuthHeaders();
|
|
64
|
-
case "apiKey": {
|
|
65
|
-
const apiKey = await strategy.apiKeyProvider.getApiKey();
|
|
66
|
-
const headerName = strategy.headerName || scheme.name || "X-API-Key";
|
|
67
|
-
if (scheme.in === "header" || !scheme.in) return { [headerName]: apiKey };
|
|
68
|
-
return {};
|
|
69
|
-
}
|
|
70
|
-
case "iam": return {};
|
|
71
|
-
default: return {};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
//#endregion
|
|
76
|
-
exports.createAuthAwareFetcher = createAuthAwareFetcher;
|
|
77
|
-
//# sourceMappingURL=auth-fetcher.cjs.map
|
|
4
|
+
exports.createAuthAwareFetcher = require_auth_fetcher.createAuthAwareFetcher;
|