@macpaw/ai-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/MIGRATION.md +52 -0
- package/README.md +272 -0
- package/dist/chunk-KGOVQRMH.js +287 -0
- package/dist/chunk-KGOVQRMH.js.map +1 -0
- package/dist/chunk-N26BDEG5.cjs +302 -0
- package/dist/chunk-N26BDEG5.cjs.map +1 -0
- package/dist/gateway-errors-DdgDIyQw.d.cts +181 -0
- package/dist/gateway-errors-DdgDIyQw.d.ts +181 -0
- package/dist/index.cjs +483 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +438 -0
- package/dist/index.js.map +1 -0
- package/dist/nestjs/index.cjs +149 -0
- package/dist/nestjs/index.cjs.map +1 -0
- package/dist/nestjs/index.d.cts +117 -0
- package/dist/nestjs/index.d.ts +117 -0
- package/dist/nestjs/index.js +145 -0
- package/dist/nestjs/index.js.map +1 -0
- package/package.json +128 -0
- package/scripts/setup.mjs +90 -0
- package/templates/AGENTS.md +51 -0
- package/templates/CLAUDE.md +55 -0
- package/templates/cursor/skills/integrate-ai-gateway/SKILL.md +90 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkN26BDEG5_cjs = require('../chunk-N26BDEG5.cjs');
|
|
4
|
+
var common = require('@nestjs/common');
|
|
5
|
+
|
|
6
|
+
// src/nestjs/ai-gateway.constants.ts
|
|
7
|
+
var AI_GATEWAY_CONFIG = /* @__PURE__ */ Symbol("AI_GATEWAY_CONFIG");
|
|
8
|
+
var AI_GATEWAY_OPTIONS = /* @__PURE__ */ Symbol("AI_GATEWAY_OPTIONS");
|
|
9
|
+
|
|
10
|
+
// src/nestjs/ai-gateway.module.ts
|
|
11
|
+
var _AIGatewayModule_decorators, _init;
|
|
12
|
+
_AIGatewayModule_decorators = [common.Module({})];
|
|
13
|
+
var _AIGatewayModule = class _AIGatewayModule {
|
|
14
|
+
/**
|
|
15
|
+
* Register the module with a static configuration.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* @Module({
|
|
20
|
+
* imports: [
|
|
21
|
+
* AIGatewayModule.forRoot({
|
|
22
|
+
* env: 'production',
|
|
23
|
+
* getAuthToken: async () => myTokenService.getToken(),
|
|
24
|
+
* }),
|
|
25
|
+
* ],
|
|
26
|
+
* })
|
|
27
|
+
* export class AppModule {}
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
static forRoot(options) {
|
|
31
|
+
const { isGlobal, ...clientConfig } = options;
|
|
32
|
+
const configProvider = {
|
|
33
|
+
provide: AI_GATEWAY_CONFIG,
|
|
34
|
+
useFactory: () => clientConfig
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
module: _AIGatewayModule,
|
|
38
|
+
global: isGlobal ?? true,
|
|
39
|
+
providers: [configProvider],
|
|
40
|
+
exports: [configProvider]
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register the module with async/dynamic configuration.
|
|
45
|
+
* Supports `useFactory`, `useClass`, and `useExisting` patterns.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* @Module({
|
|
50
|
+
* imports: [
|
|
51
|
+
* ConfigModule.forRoot(),
|
|
52
|
+
* AIGatewayModule.forRootAsync({
|
|
53
|
+
* imports: [ConfigModule],
|
|
54
|
+
* useFactory: (configService: ConfigService) => ({
|
|
55
|
+
* env: configService.get('AI_GATEWAY_ENV'),
|
|
56
|
+
* getAuthToken: async () => configService.get('AI_GATEWAY_TOKEN'),
|
|
57
|
+
* }),
|
|
58
|
+
* inject: [ConfigService],
|
|
59
|
+
* }),
|
|
60
|
+
* ],
|
|
61
|
+
* })
|
|
62
|
+
* export class AppModule {}
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
static forRootAsync(options) {
|
|
66
|
+
const configProvider = {
|
|
67
|
+
provide: AI_GATEWAY_CONFIG,
|
|
68
|
+
useFactory: ({ isGlobal: _isGlobal, ...clientConfig }) => clientConfig,
|
|
69
|
+
inject: [AI_GATEWAY_OPTIONS]
|
|
70
|
+
};
|
|
71
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
72
|
+
return {
|
|
73
|
+
module: _AIGatewayModule,
|
|
74
|
+
global: options.isGlobal ?? true,
|
|
75
|
+
imports: options.imports ?? [],
|
|
76
|
+
providers: [...asyncProviders, configProvider],
|
|
77
|
+
exports: [configProvider]
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
static createAsyncProviders(options) {
|
|
81
|
+
if (options.useFactory) {
|
|
82
|
+
return [
|
|
83
|
+
{
|
|
84
|
+
provide: AI_GATEWAY_OPTIONS,
|
|
85
|
+
useFactory: options.useFactory,
|
|
86
|
+
inject: options.inject ?? []
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
const factoryClass = options.useClass ?? options.useExisting;
|
|
91
|
+
if (!factoryClass) {
|
|
92
|
+
throw new Error("AIGatewayModule.forRootAsync() requires useFactory, useClass, or useExisting");
|
|
93
|
+
}
|
|
94
|
+
const providers = [
|
|
95
|
+
{
|
|
96
|
+
provide: AI_GATEWAY_OPTIONS,
|
|
97
|
+
useFactory: (factory) => factory.createAIGatewayOptions(),
|
|
98
|
+
inject: [factoryClass]
|
|
99
|
+
}
|
|
100
|
+
];
|
|
101
|
+
if (options.useClass) {
|
|
102
|
+
providers.push({ provide: factoryClass, useClass: factoryClass });
|
|
103
|
+
}
|
|
104
|
+
return providers;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
_init = chunkN26BDEG5_cjs.__decoratorStart(null);
|
|
108
|
+
_AIGatewayModule = chunkN26BDEG5_cjs.__decorateElement(_init, 0, "AIGatewayModule", _AIGatewayModule_decorators, _AIGatewayModule);
|
|
109
|
+
chunkN26BDEG5_cjs.__runInitializers(_init, 1, _AIGatewayModule);
|
|
110
|
+
var AIGatewayModule = _AIGatewayModule;
|
|
111
|
+
var _AIGatewayExceptionFilter_decorators, _init2;
|
|
112
|
+
_AIGatewayExceptionFilter_decorators = [common.Catch(chunkN26BDEG5_cjs.AIGatewayError)];
|
|
113
|
+
exports.AIGatewayExceptionFilter = class AIGatewayExceptionFilter {
|
|
114
|
+
catch(exception, host) {
|
|
115
|
+
const ctx = host.switchToHttp();
|
|
116
|
+
const response = ctx.getResponse();
|
|
117
|
+
const status = this.mapStatusCode(exception.statusCode);
|
|
118
|
+
const body = {
|
|
119
|
+
statusCode: status,
|
|
120
|
+
error: exception.code,
|
|
121
|
+
message: exception.message
|
|
122
|
+
};
|
|
123
|
+
if (exception.metadata?.requestId != null) body.requestId = exception.metadata.requestId;
|
|
124
|
+
if (exception.metadata?.paymentUrl != null) body.paymentUrl = exception.metadata.paymentUrl;
|
|
125
|
+
if (exception.metadata?.retryAfter != null) body.retryAfter = exception.metadata.retryAfter;
|
|
126
|
+
if (typeof response.status === "function" && typeof response.json === "function") {
|
|
127
|
+
if (exception.metadata?.retryAfter != null && typeof response.header === "function") {
|
|
128
|
+
response.header("Retry-After", String(exception.metadata.retryAfter));
|
|
129
|
+
}
|
|
130
|
+
response.status(status).json(body);
|
|
131
|
+
} else {
|
|
132
|
+
throw new common.HttpException(body, status);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
mapStatusCode(sdkStatus) {
|
|
136
|
+
if (sdkStatus >= 400 && sdkStatus < 600) return sdkStatus;
|
|
137
|
+
return common.HttpStatus.BAD_GATEWAY;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
_init2 = chunkN26BDEG5_cjs.__decoratorStart(null);
|
|
141
|
+
exports.AIGatewayExceptionFilter = chunkN26BDEG5_cjs.__decorateElement(_init2, 0, "AIGatewayExceptionFilter", _AIGatewayExceptionFilter_decorators, exports.AIGatewayExceptionFilter);
|
|
142
|
+
chunkN26BDEG5_cjs.__runInitializers(_init2, 1, exports.AIGatewayExceptionFilter);
|
|
143
|
+
var InjectAIGateway = () => common.Inject(AI_GATEWAY_CONFIG);
|
|
144
|
+
|
|
145
|
+
exports.AIGatewayModule = AIGatewayModule;
|
|
146
|
+
exports.AI_GATEWAY_CONFIG = AI_GATEWAY_CONFIG;
|
|
147
|
+
exports.InjectAIGateway = InjectAIGateway;
|
|
148
|
+
//# sourceMappingURL=index.cjs.map
|
|
149
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/nestjs/ai-gateway.constants.ts","../../src/nestjs/ai-gateway.module.ts","../../src/nestjs/ai-gateway.filter.ts","../../src/nestjs/ai-gateway.decorators.ts"],"names":["Module","__decoratorStart","__decorateElement","__runInitializers","_init","Catch","AIGatewayError","AIGatewayExceptionFilter","HttpException","HttpStatus","Inject"],"mappings":";;;;;;AACO,IAAM,iBAAA,0BAA2B,mBAAmB;AAGpD,IAAM,kBAAA,0BAA4B,oBAAoB,CAAA;;;ACJ7D,IAAA,2BAAA,EAAA,KAAA;AAMA,2BAAA,GAAA,CAACA,aAAA,CAAO,EAAE,CAAA,CAAA;AACH,IAAM,gBAAA,GAAN,MAAM,gBAAA,CAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiB3B,OAAO,QAAQ,OAAA,EAAgD;AAC7D,IAAA,MAAM,EAAE,QAAA,EAAU,GAAG,YAAA,EAAa,GAAI,OAAA;AACtC,IAAA,MAAM,cAAA,GAAoD;AAAA,MACxD,OAAA,EAAS,iBAAA;AAAA,MACT,YAAY,MAAM;AAAA,KACpB;AAEA,IAAA,OAAO;AAAA,MACL,MAAA,EAAQ,gBAAA;AAAA,MACR,QAAQ,QAAA,IAAY,IAAA;AAAA,M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,IAAA,IAAI,UAAU,QAAA,EAAU,UAAA,IAAc,MAAM,IAAA,CAAK,UAAA,GAAa,UAAU,QAAA,CAAS,UAAA;AAEjF,IAAA,IAAI,OAAO,QAAA,CAAS,MAAA,KAAW,cAAc,OAAO,QAAA,CAAS,SAAS,UAAA,EAAY;AAChF,MAAA,IAAI,UAAU,QAAA,EAAU,UAAA,IAAc,QAAQ,OAAO,QAAA,CAAS,WAAW,UAAA,EAAY;AACnF,QAAA,QAAA,CAAS,OAAO,aAAA,EAAe,MAAA,CAAO,SAAA,CAAU,QAAA,CAAS,UAAU,CAAC,CAAA;AAAA,MACtE;AACA,MAAA,QAAA,CAAS,MAAA,CAAO,MAAM,CAAA,CAAE,IAAA,CAAK,IAAI,CAAA;AAAA,IACnC,CAAA,MAAO;AACL,MAAA,MAAM,IAAIC,oBAAA,CAAc,IAAA,EAAM,MAAM,CAAA;AAAA,IACtC;AAAA,EACF;AAAA,EAEQ,cAAc,SAAA,EAA2B;AAC/C,IAAA,IAAI,SAAA,IAAa,GAAA,IAAO,SAAA,GAAY,GAAA,EAAK,OAAO,SAAA;AAChD,IAAA,OAAOC,iBAAA,CAAW,WAAA;AAAA,EACpB;AACF;AA9BOL,MAAAA,GAAAH,kCAAA,CAAA,IAAA,CAAA;AAAMM,gCAAA,GAANL,mCAAA,CAAAE,MAAAA,EAAA,CAAA,EAAA,0BAAA,EADP,oCAAA,EACaG,gCAAA,CAAA;AAANJ,mCAAA,CAAAC,QAAA,CAAA,EAAMG,gCAAA,CAAA;ACPN,IAAM,eAAA,GAAkB,MAA8CG,aAAA,CAAO,iBAAiB","file":"index.cjs","sourcesContent":["/** Injection token for the GatewayProviderSettings object. */\nexport const AI_GATEWAY_CONFIG = Symbol('AI_GATEWAY_CONFIG');\n\n/** Injection token for the module config options. */\nexport const AI_GATEWAY_OPTIONS = Symbol('AI_GATEWAY_OPTIONS');\n","import type { DynamicModule, Provider, Type } from '@nestjs/common';\nimport { Module } from '@nestjs/common';\nimport type { GatewayProviderSettings } from '../gateway-config';\nimport { AI_GATEWAY_CONFIG, AI_GATEWAY_OPTIONS } from './ai-gateway.constants';\nimport type { AIGatewayModuleOptions, AIGatewayModuleAsyncOptions, AIGatewayOptionsFactory } from './ai-gateway.types';\n\n@Module({})\nexport class AIGatewayModule {\n /**\n * Register the module with a static configuration.\n *\n * @example\n * ```ts\n * @Module({\n * imports: [\n * AIGatewayModule.forRoot({\n * env: 'production',\n * getAuthToken: async () => myTokenService.getToken(),\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n */\n static forRoot(options: AIGatewayModuleOptions): DynamicModule {\n const { isGlobal, ...clientConfig } = options;\n const configProvider: Provider<GatewayProviderSettings> = {\n provide: AI_GATEWAY_CONFIG,\n useFactory: () => clientConfig,\n };\n\n return {\n module: AIGatewayModule,\n global: isGlobal ?? true,\n providers: [configProvider],\n exports: [configProvider],\n };\n }\n\n /**\n * Register the module with async/dynamic configuration.\n * Supports `useFactory`, `useClass`, and `useExisting` patterns.\n *\n * @example\n * ```ts\n * @Module({\n * imports: [\n * ConfigModule.forRoot(),\n * AIGatewayModule.forRootAsync({\n * imports: [ConfigModule],\n * useFactory: (configService: ConfigService) => ({\n * env: configService.get('AI_GATEWAY_ENV'),\n * getAuthToken: async () => configService.get('AI_GATEWAY_TOKEN'),\n * }),\n * inject: [ConfigService],\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n */\n static forRootAsync(options: AIGatewayModuleAsyncOptions): DynamicModule {\n const configProvider: Provider<GatewayProviderSettings> = {\n provide: AI_GATEWAY_CONFIG,\n useFactory: ({ isGlobal: _isGlobal, ...clientConfig }: AIGatewayModuleOptions) =>\n clientConfig as GatewayProviderSettings,\n inject: [AI_GATEWAY_OPTIONS],\n };\n\n const asyncProviders = this.createAsyncProviders(options);\n\n return {\n module: AIGatewayModule,\n global: options.isGlobal ?? true,\n imports: options.imports ?? [],\n providers: [...asyncProviders, configProvider],\n exports: [configProvider],\n };\n }\n\n private static createAsyncProviders(options: AIGatewayModuleAsyncOptions): Provider[] {\n if (options.useFactory) {\n return [\n {\n provide: AI_GATEWAY_OPTIONS,\n useFactory: options.useFactory,\n inject: options.inject ?? [],\n },\n ];\n }\n\n const factoryClass = (options.useClass ?? options.useExisting) as Type<AIGatewayOptionsFactory> | undefined;\n if (!factoryClass) {\n throw new Error('AIGatewayModule.forRootAsync() requires useFactory, useClass, or useExisting');\n }\n\n const providers: Provider[] = [\n {\n provide: AI_GATEWAY_OPTIONS,\n useFactory: (factory: AIGatewayOptionsFactory) => factory.createAIGatewayOptions(),\n inject: [factoryClass],\n },\n ];\n\n if (options.useClass) {\n providers.push({ provide: factoryClass, useClass: factoryClass });\n }\n\n return providers;\n }\n}\n","import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';\nimport { Catch, HttpException, HttpStatus } from '@nestjs/common';\nimport { AIGatewayError } from '../gateway-errors';\n\n/**\n * NestJS exception filter that catches `AIGatewayError` and returns a structured HTTP response.\n *\n * Attach globally, per controller, or per route:\n *\n * @example\n * ```ts\n * // Global\n * app.useGlobalFilters(new AIGatewayExceptionFilter());\n *\n * // Per controller\n * @UseFilters(AIGatewayExceptionFilter)\n * @Controller('chat')\n * export class ChatController {}\n * ```\n */\n@Catch(AIGatewayError)\nexport class AIGatewayExceptionFilter implements ExceptionFilter {\n catch(exception: AIGatewayError, host: ArgumentsHost): void {\n const ctx = host.switchToHttp();\n const response = ctx.getResponse();\n\n const status = this.mapStatusCode(exception.statusCode);\n const body: Record<string, unknown> = {\n statusCode: status,\n error: exception.code,\n message: exception.message,\n };\n\n if (exception.metadata?.requestId != null) body.requestId = exception.metadata.requestId;\n if (exception.metadata?.paymentUrl != null) body.paymentUrl = exception.metadata.paymentUrl;\n if (exception.metadata?.retryAfter != null) body.retryAfter = exception.metadata.retryAfter;\n\n if (typeof response.status === 'function' && typeof response.json === 'function') {\n if (exception.metadata?.retryAfter != null && typeof response.header === 'function') {\n response.header('Retry-After', String(exception.metadata.retryAfter));\n }\n response.status(status).json(body);\n } else {\n throw new HttpException(body, status);\n }\n }\n\n private mapStatusCode(sdkStatus: number): number {\n if (sdkStatus >= 400 && sdkStatus < 600) return sdkStatus;\n return HttpStatus.BAD_GATEWAY;\n }\n}\n","import { Inject } from '@nestjs/common';\nimport { AI_GATEWAY_CONFIG } from './ai-gateway.constants';\n\n/**\n * Injects the configured `GatewayProviderSettings` into a constructor parameter or property.\n *\n * @example\n * ```ts\n * @Injectable()\n * export class ChatService {\n * constructor(@InjectAIGateway() private readonly config: GatewayProviderSettings) {}\n * }\n * ```\n */\nexport const InjectAIGateway = (): ParameterDecorator & PropertyDecorator => Inject(AI_GATEWAY_CONFIG);\n"]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ModuleMetadata, Type, InjectionToken, OptionalFactoryDependency, DynamicModule, ExceptionFilter, ArgumentsHost } from '@nestjs/common';
|
|
2
|
+
import { G as GatewayProviderSettings, A as AIGatewayError } from '../gateway-errors-DdgDIyQw.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Synchronous module configuration.
|
|
6
|
+
* Used with `AIGatewayModule.forRoot(options)`.
|
|
7
|
+
*/
|
|
8
|
+
interface AIGatewayModuleOptions extends GatewayProviderSettings {
|
|
9
|
+
/** Whether the module is global (available without importing in every module). Default: true. */
|
|
10
|
+
isGlobal?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Factory interface for creating options dynamically.
|
|
14
|
+
* Used with `AIGatewayModule.forRootAsync({ useClass: MyFactory })`.
|
|
15
|
+
*/
|
|
16
|
+
interface AIGatewayOptionsFactory {
|
|
17
|
+
createAIGatewayOptions(): Promise<AIGatewayModuleOptions> | AIGatewayModuleOptions;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Async module configuration.
|
|
21
|
+
* Used with `AIGatewayModule.forRootAsync(options)`.
|
|
22
|
+
*/
|
|
23
|
+
interface AIGatewayModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
24
|
+
/** Existing provider class that implements AIGatewayOptionsFactory. */
|
|
25
|
+
useExisting?: Type<AIGatewayOptionsFactory>;
|
|
26
|
+
/** Class to instantiate as the options factory. */
|
|
27
|
+
useClass?: Type<AIGatewayOptionsFactory>;
|
|
28
|
+
/** Factory function that returns the module options. */
|
|
29
|
+
useFactory?: (...args: unknown[]) => Promise<AIGatewayModuleOptions> | AIGatewayModuleOptions;
|
|
30
|
+
/** Dependencies to inject into useFactory. */
|
|
31
|
+
inject?: (InjectionToken | OptionalFactoryDependency)[];
|
|
32
|
+
/** Whether the module is global (available without importing in every module). Default: true. */
|
|
33
|
+
isGlobal?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare class AIGatewayModule {
|
|
37
|
+
/**
|
|
38
|
+
* Register the module with a static configuration.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* @Module({
|
|
43
|
+
* imports: [
|
|
44
|
+
* AIGatewayModule.forRoot({
|
|
45
|
+
* env: 'production',
|
|
46
|
+
* getAuthToken: async () => myTokenService.getToken(),
|
|
47
|
+
* }),
|
|
48
|
+
* ],
|
|
49
|
+
* })
|
|
50
|
+
* export class AppModule {}
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
static forRoot(options: AIGatewayModuleOptions): DynamicModule;
|
|
54
|
+
/**
|
|
55
|
+
* Register the module with async/dynamic configuration.
|
|
56
|
+
* Supports `useFactory`, `useClass`, and `useExisting` patterns.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* @Module({
|
|
61
|
+
* imports: [
|
|
62
|
+
* ConfigModule.forRoot(),
|
|
63
|
+
* AIGatewayModule.forRootAsync({
|
|
64
|
+
* imports: [ConfigModule],
|
|
65
|
+
* useFactory: (configService: ConfigService) => ({
|
|
66
|
+
* env: configService.get('AI_GATEWAY_ENV'),
|
|
67
|
+
* getAuthToken: async () => configService.get('AI_GATEWAY_TOKEN'),
|
|
68
|
+
* }),
|
|
69
|
+
* inject: [ConfigService],
|
|
70
|
+
* }),
|
|
71
|
+
* ],
|
|
72
|
+
* })
|
|
73
|
+
* export class AppModule {}
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
static forRootAsync(options: AIGatewayModuleAsyncOptions): DynamicModule;
|
|
77
|
+
private static createAsyncProviders;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* NestJS exception filter that catches `AIGatewayError` and returns a structured HTTP response.
|
|
82
|
+
*
|
|
83
|
+
* Attach globally, per controller, or per route:
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* // Global
|
|
88
|
+
* app.useGlobalFilters(new AIGatewayExceptionFilter());
|
|
89
|
+
*
|
|
90
|
+
* // Per controller
|
|
91
|
+
* @UseFilters(AIGatewayExceptionFilter)
|
|
92
|
+
* @Controller('chat')
|
|
93
|
+
* export class ChatController {}
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
declare class AIGatewayExceptionFilter implements ExceptionFilter {
|
|
97
|
+
catch(exception: AIGatewayError, host: ArgumentsHost): void;
|
|
98
|
+
private mapStatusCode;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Injects the configured `GatewayProviderSettings` into a constructor parameter or property.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* @Injectable()
|
|
107
|
+
* export class ChatService {
|
|
108
|
+
* constructor(@InjectAIGateway() private readonly config: GatewayProviderSettings) {}
|
|
109
|
+
* }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
declare const InjectAIGateway: () => ParameterDecorator & PropertyDecorator;
|
|
113
|
+
|
|
114
|
+
/** Injection token for the GatewayProviderSettings object. */
|
|
115
|
+
declare const AI_GATEWAY_CONFIG: unique symbol;
|
|
116
|
+
|
|
117
|
+
export { AIGatewayExceptionFilter, AIGatewayModule, type AIGatewayModuleAsyncOptions, type AIGatewayModuleOptions, type AIGatewayOptionsFactory, AI_GATEWAY_CONFIG, InjectAIGateway };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ModuleMetadata, Type, InjectionToken, OptionalFactoryDependency, DynamicModule, ExceptionFilter, ArgumentsHost } from '@nestjs/common';
|
|
2
|
+
import { G as GatewayProviderSettings, A as AIGatewayError } from '../gateway-errors-DdgDIyQw.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Synchronous module configuration.
|
|
6
|
+
* Used with `AIGatewayModule.forRoot(options)`.
|
|
7
|
+
*/
|
|
8
|
+
interface AIGatewayModuleOptions extends GatewayProviderSettings {
|
|
9
|
+
/** Whether the module is global (available without importing in every module). Default: true. */
|
|
10
|
+
isGlobal?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Factory interface for creating options dynamically.
|
|
14
|
+
* Used with `AIGatewayModule.forRootAsync({ useClass: MyFactory })`.
|
|
15
|
+
*/
|
|
16
|
+
interface AIGatewayOptionsFactory {
|
|
17
|
+
createAIGatewayOptions(): Promise<AIGatewayModuleOptions> | AIGatewayModuleOptions;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Async module configuration.
|
|
21
|
+
* Used with `AIGatewayModule.forRootAsync(options)`.
|
|
22
|
+
*/
|
|
23
|
+
interface AIGatewayModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
24
|
+
/** Existing provider class that implements AIGatewayOptionsFactory. */
|
|
25
|
+
useExisting?: Type<AIGatewayOptionsFactory>;
|
|
26
|
+
/** Class to instantiate as the options factory. */
|
|
27
|
+
useClass?: Type<AIGatewayOptionsFactory>;
|
|
28
|
+
/** Factory function that returns the module options. */
|
|
29
|
+
useFactory?: (...args: unknown[]) => Promise<AIGatewayModuleOptions> | AIGatewayModuleOptions;
|
|
30
|
+
/** Dependencies to inject into useFactory. */
|
|
31
|
+
inject?: (InjectionToken | OptionalFactoryDependency)[];
|
|
32
|
+
/** Whether the module is global (available without importing in every module). Default: true. */
|
|
33
|
+
isGlobal?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare class AIGatewayModule {
|
|
37
|
+
/**
|
|
38
|
+
* Register the module with a static configuration.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* @Module({
|
|
43
|
+
* imports: [
|
|
44
|
+
* AIGatewayModule.forRoot({
|
|
45
|
+
* env: 'production',
|
|
46
|
+
* getAuthToken: async () => myTokenService.getToken(),
|
|
47
|
+
* }),
|
|
48
|
+
* ],
|
|
49
|
+
* })
|
|
50
|
+
* export class AppModule {}
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
static forRoot(options: AIGatewayModuleOptions): DynamicModule;
|
|
54
|
+
/**
|
|
55
|
+
* Register the module with async/dynamic configuration.
|
|
56
|
+
* Supports `useFactory`, `useClass`, and `useExisting` patterns.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* @Module({
|
|
61
|
+
* imports: [
|
|
62
|
+
* ConfigModule.forRoot(),
|
|
63
|
+
* AIGatewayModule.forRootAsync({
|
|
64
|
+
* imports: [ConfigModule],
|
|
65
|
+
* useFactory: (configService: ConfigService) => ({
|
|
66
|
+
* env: configService.get('AI_GATEWAY_ENV'),
|
|
67
|
+
* getAuthToken: async () => configService.get('AI_GATEWAY_TOKEN'),
|
|
68
|
+
* }),
|
|
69
|
+
* inject: [ConfigService],
|
|
70
|
+
* }),
|
|
71
|
+
* ],
|
|
72
|
+
* })
|
|
73
|
+
* export class AppModule {}
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
static forRootAsync(options: AIGatewayModuleAsyncOptions): DynamicModule;
|
|
77
|
+
private static createAsyncProviders;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* NestJS exception filter that catches `AIGatewayError` and returns a structured HTTP response.
|
|
82
|
+
*
|
|
83
|
+
* Attach globally, per controller, or per route:
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* // Global
|
|
88
|
+
* app.useGlobalFilters(new AIGatewayExceptionFilter());
|
|
89
|
+
*
|
|
90
|
+
* // Per controller
|
|
91
|
+
* @UseFilters(AIGatewayExceptionFilter)
|
|
92
|
+
* @Controller('chat')
|
|
93
|
+
* export class ChatController {}
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
declare class AIGatewayExceptionFilter implements ExceptionFilter {
|
|
97
|
+
catch(exception: AIGatewayError, host: ArgumentsHost): void;
|
|
98
|
+
private mapStatusCode;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Injects the configured `GatewayProviderSettings` into a constructor parameter or property.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* @Injectable()
|
|
107
|
+
* export class ChatService {
|
|
108
|
+
* constructor(@InjectAIGateway() private readonly config: GatewayProviderSettings) {}
|
|
109
|
+
* }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
declare const InjectAIGateway: () => ParameterDecorator & PropertyDecorator;
|
|
113
|
+
|
|
114
|
+
/** Injection token for the GatewayProviderSettings object. */
|
|
115
|
+
declare const AI_GATEWAY_CONFIG: unique symbol;
|
|
116
|
+
|
|
117
|
+
export { AIGatewayExceptionFilter, AIGatewayModule, type AIGatewayModuleAsyncOptions, type AIGatewayModuleOptions, type AIGatewayOptionsFactory, AI_GATEWAY_CONFIG, InjectAIGateway };
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { __decoratorStart, __decorateElement, __runInitializers, AIGatewayError } from '../chunk-KGOVQRMH.js';
|
|
2
|
+
import { Module, Catch, HttpException, HttpStatus, Inject } from '@nestjs/common';
|
|
3
|
+
|
|
4
|
+
// src/nestjs/ai-gateway.constants.ts
|
|
5
|
+
var AI_GATEWAY_CONFIG = /* @__PURE__ */ Symbol("AI_GATEWAY_CONFIG");
|
|
6
|
+
var AI_GATEWAY_OPTIONS = /* @__PURE__ */ Symbol("AI_GATEWAY_OPTIONS");
|
|
7
|
+
|
|
8
|
+
// src/nestjs/ai-gateway.module.ts
|
|
9
|
+
var _AIGatewayModule_decorators, _init;
|
|
10
|
+
_AIGatewayModule_decorators = [Module({})];
|
|
11
|
+
var _AIGatewayModule = class _AIGatewayModule {
|
|
12
|
+
/**
|
|
13
|
+
* Register the module with a static configuration.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* @Module({
|
|
18
|
+
* imports: [
|
|
19
|
+
* AIGatewayModule.forRoot({
|
|
20
|
+
* env: 'production',
|
|
21
|
+
* getAuthToken: async () => myTokenService.getToken(),
|
|
22
|
+
* }),
|
|
23
|
+
* ],
|
|
24
|
+
* })
|
|
25
|
+
* export class AppModule {}
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
static forRoot(options) {
|
|
29
|
+
const { isGlobal, ...clientConfig } = options;
|
|
30
|
+
const configProvider = {
|
|
31
|
+
provide: AI_GATEWAY_CONFIG,
|
|
32
|
+
useFactory: () => clientConfig
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
module: _AIGatewayModule,
|
|
36
|
+
global: isGlobal ?? true,
|
|
37
|
+
providers: [configProvider],
|
|
38
|
+
exports: [configProvider]
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Register the module with async/dynamic configuration.
|
|
43
|
+
* Supports `useFactory`, `useClass`, and `useExisting` patterns.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* @Module({
|
|
48
|
+
* imports: [
|
|
49
|
+
* ConfigModule.forRoot(),
|
|
50
|
+
* AIGatewayModule.forRootAsync({
|
|
51
|
+
* imports: [ConfigModule],
|
|
52
|
+
* useFactory: (configService: ConfigService) => ({
|
|
53
|
+
* env: configService.get('AI_GATEWAY_ENV'),
|
|
54
|
+
* getAuthToken: async () => configService.get('AI_GATEWAY_TOKEN'),
|
|
55
|
+
* }),
|
|
56
|
+
* inject: [ConfigService],
|
|
57
|
+
* }),
|
|
58
|
+
* ],
|
|
59
|
+
* })
|
|
60
|
+
* export class AppModule {}
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
static forRootAsync(options) {
|
|
64
|
+
const configProvider = {
|
|
65
|
+
provide: AI_GATEWAY_CONFIG,
|
|
66
|
+
useFactory: ({ isGlobal: _isGlobal, ...clientConfig }) => clientConfig,
|
|
67
|
+
inject: [AI_GATEWAY_OPTIONS]
|
|
68
|
+
};
|
|
69
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
70
|
+
return {
|
|
71
|
+
module: _AIGatewayModule,
|
|
72
|
+
global: options.isGlobal ?? true,
|
|
73
|
+
imports: options.imports ?? [],
|
|
74
|
+
providers: [...asyncProviders, configProvider],
|
|
75
|
+
exports: [configProvider]
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
static createAsyncProviders(options) {
|
|
79
|
+
if (options.useFactory) {
|
|
80
|
+
return [
|
|
81
|
+
{
|
|
82
|
+
provide: AI_GATEWAY_OPTIONS,
|
|
83
|
+
useFactory: options.useFactory,
|
|
84
|
+
inject: options.inject ?? []
|
|
85
|
+
}
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
const factoryClass = options.useClass ?? options.useExisting;
|
|
89
|
+
if (!factoryClass) {
|
|
90
|
+
throw new Error("AIGatewayModule.forRootAsync() requires useFactory, useClass, or useExisting");
|
|
91
|
+
}
|
|
92
|
+
const providers = [
|
|
93
|
+
{
|
|
94
|
+
provide: AI_GATEWAY_OPTIONS,
|
|
95
|
+
useFactory: (factory) => factory.createAIGatewayOptions(),
|
|
96
|
+
inject: [factoryClass]
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
if (options.useClass) {
|
|
100
|
+
providers.push({ provide: factoryClass, useClass: factoryClass });
|
|
101
|
+
}
|
|
102
|
+
return providers;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
_init = __decoratorStart(null);
|
|
106
|
+
_AIGatewayModule = __decorateElement(_init, 0, "AIGatewayModule", _AIGatewayModule_decorators, _AIGatewayModule);
|
|
107
|
+
__runInitializers(_init, 1, _AIGatewayModule);
|
|
108
|
+
var AIGatewayModule = _AIGatewayModule;
|
|
109
|
+
var _AIGatewayExceptionFilter_decorators, _init2;
|
|
110
|
+
_AIGatewayExceptionFilter_decorators = [Catch(AIGatewayError)];
|
|
111
|
+
var AIGatewayExceptionFilter = class {
|
|
112
|
+
catch(exception, host) {
|
|
113
|
+
const ctx = host.switchToHttp();
|
|
114
|
+
const response = ctx.getResponse();
|
|
115
|
+
const status = this.mapStatusCode(exception.statusCode);
|
|
116
|
+
const body = {
|
|
117
|
+
statusCode: status,
|
|
118
|
+
error: exception.code,
|
|
119
|
+
message: exception.message
|
|
120
|
+
};
|
|
121
|
+
if (exception.metadata?.requestId != null) body.requestId = exception.metadata.requestId;
|
|
122
|
+
if (exception.metadata?.paymentUrl != null) body.paymentUrl = exception.metadata.paymentUrl;
|
|
123
|
+
if (exception.metadata?.retryAfter != null) body.retryAfter = exception.metadata.retryAfter;
|
|
124
|
+
if (typeof response.status === "function" && typeof response.json === "function") {
|
|
125
|
+
if (exception.metadata?.retryAfter != null && typeof response.header === "function") {
|
|
126
|
+
response.header("Retry-After", String(exception.metadata.retryAfter));
|
|
127
|
+
}
|
|
128
|
+
response.status(status).json(body);
|
|
129
|
+
} else {
|
|
130
|
+
throw new HttpException(body, status);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
mapStatusCode(sdkStatus) {
|
|
134
|
+
if (sdkStatus >= 400 && sdkStatus < 600) return sdkStatus;
|
|
135
|
+
return HttpStatus.BAD_GATEWAY;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
_init2 = __decoratorStart(null);
|
|
139
|
+
AIGatewayExceptionFilter = __decorateElement(_init2, 0, "AIGatewayExceptionFilter", _AIGatewayExceptionFilter_decorators, AIGatewayExceptionFilter);
|
|
140
|
+
__runInitializers(_init2, 1, AIGatewayExceptionFilter);
|
|
141
|
+
var InjectAIGateway = () => Inject(AI_GATEWAY_CONFIG);
|
|
142
|
+
|
|
143
|
+
export { AIGatewayExceptionFilter, AIGatewayModule, AI_GATEWAY_CONFIG, InjectAIGateway };
|
|
144
|
+
//# sourceMappingURL=index.js.map
|
|
145
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/nestjs/ai-gateway.constants.ts","../../src/nestjs/ai-gateway.module.ts","../../src/nestjs/ai-gateway.filter.ts","../../src/nestjs/ai-gateway.decorators.ts"],"names":["_init"],"mappings":";;;;AACO,IAAM,iBAAA,0BAA2B,mBAAmB;AAGpD,IAAM,kBAAA,0BAA4B,oBAAoB,CAAA;;;ACJ7D,IAAA,2BAAA,EAAA,KAAA;AAMA,2BAAA,GAAA,CAAC,MAAA,CAAO,EAAE,CAAA,CAAA;AACH,IAAM,gBAAA,GAAN,MAAM,gBAAA,CAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiB3B,OAAO,QAAQ,OAAA,EAAgD;AAC7D,IAAA,MAAM,EAAE,QAAA,EAAU,GAAG,YAAA,EAAa,GAAI,OAAA;AACtC,IAAA,MAAM,cAAA,GAAoD;AAAA,MACxD,OAAA,EAAS,iBAAA;AAAA,MACT,YAAY,MAAM;AAAA,KACpB;AAEA,IAAA,OAAO;AAAA,MACL,MAAA,EAAQ,gBAAA;AAAA,MACR,QAAQ,QAAA,IAAY,IAAA;AAAA,MACpB,SAAA,EAAW,CAAC,cAAc,CAAA;AAAA,MAC1B,OAAA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K,UAAA,GAAa,UAAU,QAAA,CAAS,UAAA;AAEjF,IAAA,IAAI,OAAO,QAAA,CAAS,MAAA,KAAW,cAAc,OAAO,QAAA,CAAS,SAAS,UAAA,EAAY;AAChF,MAAA,IAAI,UAAU,QAAA,EAAU,UAAA,IAAc,QAAQ,OAAO,QAAA,CAAS,WAAW,UAAA,EAAY;AACnF,QAAA,QAAA,CAAS,OAAO,aAAA,EAAe,MAAA,CAAO,SAAA,CAAU,QAAA,CAAS,UAAU,CAAC,CAAA;AAAA,MACtE;AACA,MAAA,QAAA,CAAS,MAAA,CAAO,MAAM,CAAA,CAAE,IAAA,CAAK,IAAI,CAAA;AAAA,IACnC,CAAA,MAAO;AACL,MAAA,MAAM,IAAI,aAAA,CAAc,IAAA,EAAM,MAAM,CAAA;AAAA,IACtC;AAAA,EACF;AAAA,EAEQ,cAAc,SAAA,EAA2B;AAC/C,IAAA,IAAI,SAAA,IAAa,GAAA,IAAO,SAAA,GAAY,GAAA,EAAK,OAAO,SAAA;AAChD,IAAA,OAAO,UAAA,CAAW,WAAA;AAAA,EACpB;AACF;AA9BOA,MAAAA,GAAA,gBAAA,CAAA,IAAA,CAAA;AAAM,wBAAA,GAAN,iBAAA,CAAAA,MAAAA,EAAA,CAAA,EAAA,0BAAA,EADP,oCAAA,EACa,wBAAA,CAAA;AAAN,iBAAA,CAAAA,QAAA,CAAA,EAAM,wBAAA,CAAA;ACPN,IAAM,eAAA,GAAkB,MAA8C,MAAA,CAAO,iBAAiB","file":"index.js","sourcesContent":["/** Injection token for the GatewayProviderSettings object. */\nexport const AI_GATEWAY_CONFIG = Symbol('AI_GATEWAY_CONFIG');\n\n/** Injection token for the module config options. */\nexport const AI_GATEWAY_OPTIONS = Symbol('AI_GATEWAY_OPTIONS');\n","import type { DynamicModule, Provider, Type } from '@nestjs/common';\nimport { Module } from '@nestjs/common';\nimport type { GatewayProviderSettings } from '../gateway-config';\nimport { AI_GATEWAY_CONFIG, AI_GATEWAY_OPTIONS } from './ai-gateway.constants';\nimport type { AIGatewayModuleOptions, AIGatewayModuleAsyncOptions, AIGatewayOptionsFactory } from './ai-gateway.types';\n\n@Module({})\nexport class AIGatewayModule {\n /**\n * Register the module with a static configuration.\n *\n * @example\n * ```ts\n * @Module({\n * imports: [\n * AIGatewayModule.forRoot({\n * env: 'production',\n * getAuthToken: async () => myTokenService.getToken(),\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n */\n static forRoot(options: AIGatewayModuleOptions): DynamicModule {\n const { isGlobal, ...clientConfig } = options;\n const configProvider: Provider<GatewayProviderSettings> = {\n provide: AI_GATEWAY_CONFIG,\n useFactory: () => clientConfig,\n };\n\n return {\n module: AIGatewayModule,\n global: isGlobal ?? true,\n providers: [configProvider],\n exports: [configProvider],\n };\n }\n\n /**\n * Register the module with async/dynamic configuration.\n * Supports `useFactory`, `useClass`, and `useExisting` patterns.\n *\n * @example\n * ```ts\n * @Module({\n * imports: [\n * ConfigModule.forRoot(),\n * AIGatewayModule.forRootAsync({\n * imports: [ConfigModule],\n * useFactory: (configService: ConfigService) => ({\n * env: configService.get('AI_GATEWAY_ENV'),\n * getAuthToken: async () => configService.get('AI_GATEWAY_TOKEN'),\n * }),\n * inject: [ConfigService],\n * }),\n * ],\n * })\n * export class AppModule {}\n * ```\n */\n static forRootAsync(options: AIGatewayModuleAsyncOptions): DynamicModule {\n const configProvider: Provider<GatewayProviderSettings> = {\n provide: AI_GATEWAY_CONFIG,\n useFactory: ({ isGlobal: _isGlobal, ...clientConfig }: AIGatewayModuleOptions) =>\n clientConfig as GatewayProviderSettings,\n inject: [AI_GATEWAY_OPTIONS],\n };\n\n const asyncProviders = this.createAsyncProviders(options);\n\n return {\n module: AIGatewayModule,\n global: options.isGlobal ?? true,\n imports: options.imports ?? [],\n providers: [...asyncProviders, configProvider],\n exports: [configProvider],\n };\n }\n\n private static createAsyncProviders(options: AIGatewayModuleAsyncOptions): Provider[] {\n if (options.useFactory) {\n return [\n {\n provide: AI_GATEWAY_OPTIONS,\n useFactory: options.useFactory,\n inject: options.inject ?? [],\n },\n ];\n }\n\n const factoryClass = (options.useClass ?? options.useExisting) as Type<AIGatewayOptionsFactory> | undefined;\n if (!factoryClass) {\n throw new Error('AIGatewayModule.forRootAsync() requires useFactory, useClass, or useExisting');\n }\n\n const providers: Provider[] = [\n {\n provide: AI_GATEWAY_OPTIONS,\n useFactory: (factory: AIGatewayOptionsFactory) => factory.createAIGatewayOptions(),\n inject: [factoryClass],\n },\n ];\n\n if (options.useClass) {\n providers.push({ provide: factoryClass, useClass: factoryClass });\n }\n\n return providers;\n }\n}\n","import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';\nimport { Catch, HttpException, HttpStatus } from '@nestjs/common';\nimport { AIGatewayError } from '../gateway-errors';\n\n/**\n * NestJS exception filter that catches `AIGatewayError` and returns a structured HTTP response.\n *\n * Attach globally, per controller, or per route:\n *\n * @example\n * ```ts\n * // Global\n * app.useGlobalFilters(new AIGatewayExceptionFilter());\n *\n * // Per controller\n * @UseFilters(AIGatewayExceptionFilter)\n * @Controller('chat')\n * export class ChatController {}\n * ```\n */\n@Catch(AIGatewayError)\nexport class AIGatewayExceptionFilter implements ExceptionFilter {\n catch(exception: AIGatewayError, host: ArgumentsHost): void {\n const ctx = host.switchToHttp();\n const response = ctx.getResponse();\n\n const status = this.mapStatusCode(exception.statusCode);\n const body: Record<string, unknown> = {\n statusCode: status,\n error: exception.code,\n message: exception.message,\n };\n\n if (exception.metadata?.requestId != null) body.requestId = exception.metadata.requestId;\n if (exception.metadata?.paymentUrl != null) body.paymentUrl = exception.metadata.paymentUrl;\n if (exception.metadata?.retryAfter != null) body.retryAfter = exception.metadata.retryAfter;\n\n if (typeof response.status === 'function' && typeof response.json === 'function') {\n if (exception.metadata?.retryAfter != null && typeof response.header === 'function') {\n response.header('Retry-After', String(exception.metadata.retryAfter));\n }\n response.status(status).json(body);\n } else {\n throw new HttpException(body, status);\n }\n }\n\n private mapStatusCode(sdkStatus: number): number {\n if (sdkStatus >= 400 && sdkStatus < 600) return sdkStatus;\n return HttpStatus.BAD_GATEWAY;\n }\n}\n","import { Inject } from '@nestjs/common';\nimport { AI_GATEWAY_CONFIG } from './ai-gateway.constants';\n\n/**\n * Injects the configured `GatewayProviderSettings` into a constructor parameter or property.\n *\n * @example\n * ```ts\n * @Injectable()\n * export class ChatService {\n * constructor(@InjectAIGateway() private readonly config: GatewayProviderSettings) {}\n * }\n * ```\n */\nexport const InjectAIGateway = (): ParameterDecorator & PropertyDecorator => Inject(AI_GATEWAY_CONFIG);\n"]}
|