@channel.io/app-sdk-server 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/dist/__tests__/decorators.test.d.ts +2 -0
- package/dist/__tests__/decorators.test.d.ts.map +1 -0
- package/dist/__tests__/decorators.test.js +259 -0
- package/dist/__tests__/decorators.test.js.map +1 -0
- package/dist/__tests__/testing-utils.test.d.ts +2 -0
- package/dist/__tests__/testing-utils.test.d.ts.map +1 -0
- package/dist/__tests__/testing-utils.test.js +118 -0
- package/dist/__tests__/testing-utils.test.js.map +1 -0
- package/dist/appstore/client.d.ts +70 -0
- package/dist/appstore/client.d.ts.map +1 -0
- package/dist/appstore/client.js +141 -0
- package/dist/appstore/client.js.map +1 -0
- package/dist/appstore/index.d.ts +3 -0
- package/dist/appstore/index.d.ts.map +1 -0
- package/dist/appstore/index.js +3 -0
- package/dist/appstore/index.js.map +1 -0
- package/dist/appstore/types.d.ts +87 -0
- package/dist/appstore/types.d.ts.map +1 -0
- package/dist/appstore/types.js +5 -0
- package/dist/appstore/types.js.map +1 -0
- package/dist/decorators/extension.decorator.d.ts +33 -0
- package/dist/decorators/extension.decorator.d.ts.map +1 -0
- package/dist/decorators/extension.decorator.js +36 -0
- package/dist/decorators/extension.decorator.js.map +1 -0
- package/dist/decorators/function.decorator.d.ts +34 -0
- package/dist/decorators/function.decorator.d.ts.map +1 -0
- package/dist/decorators/function.decorator.js +45 -0
- package/dist/decorators/function.decorator.js.map +1 -0
- package/dist/decorators/index.d.ts +5 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +9 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/param.decorator.d.ts +49 -0
- package/dist/decorators/param.decorator.d.ts.map +1 -0
- package/dist/decorators/param.decorator.js +62 -0
- package/dist/decorators/param.decorator.js.map +1 -0
- package/dist/decorators/schema.decorator.d.ts +48 -0
- package/dist/decorators/schema.decorator.d.ts.map +1 -0
- package/dist/decorators/schema.decorator.js +59 -0
- package/dist/decorators/schema.decorator.js.map +1 -0
- package/dist/discovery/extension-discovery.service.d.ts +45 -0
- package/dist/discovery/extension-discovery.service.d.ts.map +1 -0
- package/dist/discovery/extension-discovery.service.js +192 -0
- package/dist/discovery/extension-discovery.service.js.map +1 -0
- package/dist/discovery/index.d.ts +3 -0
- package/dist/discovery/index.d.ts.map +1 -0
- package/dist/discovery/index.js +2 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/metadata.interface.d.ts +42 -0
- package/dist/discovery/metadata.interface.d.ts.map +1 -0
- package/dist/discovery/metadata.interface.js +2 -0
- package/dist/discovery/metadata.interface.js.map +1 -0
- package/dist/guards/index.d.ts +3 -0
- package/dist/guards/index.d.ts.map +1 -0
- package/dist/guards/index.js +3 -0
- package/dist/guards/index.js.map +1 -0
- package/dist/guards/signature.guard.d.ts +65 -0
- package/dist/guards/signature.guard.d.ts.map +1 -0
- package/dist/guards/signature.guard.js +156 -0
- package/dist/guards/signature.guard.js.map +1 -0
- package/dist/guards/signature.middleware.d.ts +76 -0
- package/dist/guards/signature.middleware.d.ts.map +1 -0
- package/dist/guards/signature.middleware.js +110 -0
- package/dist/guards/signature.middleware.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/native/client.d.ts +105 -0
- package/dist/native/client.d.ts.map +1 -0
- package/dist/native/client.js +210 -0
- package/dist/native/client.js.map +1 -0
- package/dist/native/index.d.ts +3 -0
- package/dist/native/index.d.ts.map +1 -0
- package/dist/native/index.js +3 -0
- package/dist/native/index.js.map +1 -0
- package/dist/native/types.d.ts +198 -0
- package/dist/native/types.d.ts.map +1 -0
- package/dist/native/types.js +8 -0
- package/dist/native/types.js.map +1 -0
- package/dist/nestjs/channel-app.controller.d.ts +22 -0
- package/dist/nestjs/channel-app.controller.d.ts.map +1 -0
- package/dist/nestjs/channel-app.controller.js +87 -0
- package/dist/nestjs/channel-app.controller.js.map +1 -0
- package/dist/nestjs/channel-app.module.d.ts +47 -0
- package/dist/nestjs/channel-app.module.d.ts.map +1 -0
- package/dist/nestjs/channel-app.module.js +93 -0
- package/dist/nestjs/channel-app.module.js.map +1 -0
- package/dist/nestjs/channel-app.service.d.ts +44 -0
- package/dist/nestjs/channel-app.service.d.ts.map +1 -0
- package/dist/nestjs/channel-app.service.js +190 -0
- package/dist/nestjs/channel-app.service.js.map +1 -0
- package/dist/nestjs/index.d.ts +5 -0
- package/dist/nestjs/index.d.ts.map +1 -0
- package/dist/nestjs/index.js +5 -0
- package/dist/nestjs/index.js.map +1 -0
- package/dist/nestjs/types.d.ts +70 -0
- package/dist/nestjs/types.d.ts.map +1 -0
- package/dist/nestjs/types.js +5 -0
- package/dist/nestjs/types.js.map +1 -0
- package/dist/simple/channel-app.d.ts +80 -0
- package/dist/simple/channel-app.d.ts.map +1 -0
- package/dist/simple/channel-app.js +253 -0
- package/dist/simple/channel-app.js.map +1 -0
- package/dist/simple/index.d.ts +2 -0
- package/dist/simple/index.d.ts.map +1 -0
- package/dist/simple/index.js +2 -0
- package/dist/simple/index.js.map +1 -0
- package/dist/testing/index.d.ts +9 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +9 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mock-context.d.ts +32 -0
- package/dist/testing/mock-context.d.ts.map +1 -0
- package/dist/testing/mock-context.js +33 -0
- package/dist/testing/mock-context.js.map +1 -0
- package/dist/testing/test-extension.d.ts +60 -0
- package/dist/testing/test-extension.d.ts.map +1 -0
- package/dist/testing/test-extension.js +113 -0
- package/dist/testing/test-extension.js.map +1 -0
- package/dist/token/cache.d.ts +31 -0
- package/dist/token/cache.d.ts.map +1 -0
- package/dist/token/cache.js +69 -0
- package/dist/token/cache.js.map +1 -0
- package/dist/token/index.d.ts +4 -0
- package/dist/token/index.d.ts.map +1 -0
- package/dist/token/index.js +4 -0
- package/dist/token/index.js.map +1 -0
- package/dist/token/manager.d.ts +111 -0
- package/dist/token/manager.d.ts.map +1 -0
- package/dist/token/manager.js +246 -0
- package/dist/token/manager.js.map +1 -0
- package/dist/token/types.d.ts +85 -0
- package/dist/token/types.d.ts.map +1 -0
- package/dist/token/types.js +5 -0
- package/dist/token/types.js.map +1 -0
- package/package.json +69 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
var ChannelAppService_1;
|
|
14
|
+
import { Inject, Injectable, Logger, Optional } from "@nestjs/common";
|
|
15
|
+
import { FunctionNotFoundError, } from "@channel.io/app-sdk-core";
|
|
16
|
+
import { CHANNEL_APP_OPTIONS, } from "./types.js";
|
|
17
|
+
import { AppStoreClient } from "../appstore/client.js";
|
|
18
|
+
import { ExtensionDiscoveryService } from "../discovery/extension-discovery.service.js";
|
|
19
|
+
/**
|
|
20
|
+
* Version mismatch error for when a function call targets the wrong version
|
|
21
|
+
*/
|
|
22
|
+
export class VersionMismatchError extends Error {
|
|
23
|
+
requestedVersion;
|
|
24
|
+
availableVersions;
|
|
25
|
+
constructor(requestedVersion, availableVersions) {
|
|
26
|
+
super(`Version '${requestedVersion}' not found. Available versions: ${availableVersions.join(", ")}`);
|
|
27
|
+
this.requestedVersion = requestedVersion;
|
|
28
|
+
this.availableVersions = availableVersions;
|
|
29
|
+
this.name = "VersionMismatchError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let ChannelAppService = ChannelAppService_1 = class ChannelAppService {
|
|
33
|
+
options;
|
|
34
|
+
discoveryService;
|
|
35
|
+
logger = new Logger(ChannelAppService_1.name);
|
|
36
|
+
constructor(options, discoveryService) {
|
|
37
|
+
this.options = options;
|
|
38
|
+
this.discoveryService = discoveryService;
|
|
39
|
+
}
|
|
40
|
+
async onModuleInit() {
|
|
41
|
+
// Log discovered extensions
|
|
42
|
+
if (this.discoveryService?.hasExtensions()) {
|
|
43
|
+
const extensions = this.discoveryService.getExtensions();
|
|
44
|
+
this.logger.log(`Discovered ${extensions.length} extension(s) via decorators`);
|
|
45
|
+
for (const ext of extensions) {
|
|
46
|
+
this.logger.log(` - ${ext.name} (${ext.systemVersion}): ${ext.functions.length} function(s)`);
|
|
47
|
+
if (this.options.debug) {
|
|
48
|
+
for (const func of ext.functions) {
|
|
49
|
+
this.logger.debug(` - ${func.fullName}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Auto-register extensions with AppStore if enabled
|
|
55
|
+
if (this.options.autoRegister) {
|
|
56
|
+
await this.autoRegisterExtensions();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Auto-register all extensions with the AppStore
|
|
61
|
+
*/
|
|
62
|
+
async autoRegisterExtensions() {
|
|
63
|
+
const client = new AppStoreClient({
|
|
64
|
+
appId: this.options.appId,
|
|
65
|
+
appSecret: this.options.appSecret,
|
|
66
|
+
appStoreUrl: this.options.appStoreUrl,
|
|
67
|
+
debug: this.options.debug,
|
|
68
|
+
});
|
|
69
|
+
const results = [];
|
|
70
|
+
const extensions = this.discoveryService?.getExtensions() ?? [];
|
|
71
|
+
for (const extension of extensions) {
|
|
72
|
+
const systemVersion = extension.systemVersion;
|
|
73
|
+
try {
|
|
74
|
+
this.logger.log(`Auto-registering extension: ${extension.name} (${systemVersion})`);
|
|
75
|
+
const result = await client.registerExtension({
|
|
76
|
+
extensionName: extension.name,
|
|
77
|
+
systemVersion,
|
|
78
|
+
});
|
|
79
|
+
if (result.success) {
|
|
80
|
+
this.logger.log(`Successfully registered extension: ${extension.name}`);
|
|
81
|
+
results.push({
|
|
82
|
+
extensionName: extension.name,
|
|
83
|
+
systemVersion,
|
|
84
|
+
success: true,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const errorMsg = result.error_message ?? result.validation_errors?.join(", ") ?? "Unknown error";
|
|
89
|
+
this.logger.warn(`Failed to register extension ${extension.name}: ${errorMsg}`);
|
|
90
|
+
results.push({
|
|
91
|
+
extensionName: extension.name,
|
|
92
|
+
systemVersion,
|
|
93
|
+
success: false,
|
|
94
|
+
error: errorMsg,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
100
|
+
this.logger.error(`Error registering extension ${extension.name}: ${errorMsg}`);
|
|
101
|
+
results.push({
|
|
102
|
+
extensionName: extension.name,
|
|
103
|
+
systemVersion,
|
|
104
|
+
success: false,
|
|
105
|
+
error: errorMsg,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Call callback if provided
|
|
110
|
+
if (this.options.onAutoRegister) {
|
|
111
|
+
this.options.onAutoRegister(results);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get all registered versions
|
|
116
|
+
*/
|
|
117
|
+
getRegisteredVersions() {
|
|
118
|
+
const versions = new Set();
|
|
119
|
+
const extensions = this.discoveryService?.getExtensions() ?? [];
|
|
120
|
+
for (const ext of extensions) {
|
|
121
|
+
versions.add(ext.systemVersion);
|
|
122
|
+
}
|
|
123
|
+
return Array.from(versions);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Handle incoming function call
|
|
127
|
+
* @param request The function call request
|
|
128
|
+
* @param _version Optional version from URL path (e.g., "v1")
|
|
129
|
+
*/
|
|
130
|
+
async handleFunctionCall(request, _version) {
|
|
131
|
+
const { method, context, params } = request;
|
|
132
|
+
// Handle getFunctions request
|
|
133
|
+
if (method === "extension.core.function.getFunctions") {
|
|
134
|
+
return this.getFunctions();
|
|
135
|
+
}
|
|
136
|
+
// Find the function from discovery service
|
|
137
|
+
const func = this.discoveryService?.getFunction(method);
|
|
138
|
+
if (!func) {
|
|
139
|
+
throw new FunctionNotFoundError(method);
|
|
140
|
+
}
|
|
141
|
+
if (this.options.debug) {
|
|
142
|
+
this.logger.debug(`Calling function: ${method}`);
|
|
143
|
+
}
|
|
144
|
+
// Execute the handler
|
|
145
|
+
const result = await func.handler(context, params ?? {});
|
|
146
|
+
return { result };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get list of all registered functions
|
|
150
|
+
*/
|
|
151
|
+
getFunctions() {
|
|
152
|
+
const allFunctions = this.discoveryService?.getAllFunctions() ?? [];
|
|
153
|
+
const functions = allFunctions.map((func) => {
|
|
154
|
+
const schema = {
|
|
155
|
+
name: func.fullName,
|
|
156
|
+
inputSchema: func.inputJsonSchema ?? { type: "object" },
|
|
157
|
+
};
|
|
158
|
+
if (func.description) {
|
|
159
|
+
schema.description = func.description;
|
|
160
|
+
}
|
|
161
|
+
if (func.outputJsonSchema) {
|
|
162
|
+
schema.outputSchema = func.outputJsonSchema;
|
|
163
|
+
}
|
|
164
|
+
return schema;
|
|
165
|
+
});
|
|
166
|
+
return {
|
|
167
|
+
result: {
|
|
168
|
+
functions,
|
|
169
|
+
success: true,
|
|
170
|
+
errorMessage: "",
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get app configuration
|
|
176
|
+
*/
|
|
177
|
+
getAppConfig() {
|
|
178
|
+
return {
|
|
179
|
+
appId: this.options.appId,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
ChannelAppService = ChannelAppService_1 = __decorate([
|
|
184
|
+
Injectable(),
|
|
185
|
+
__param(0, Inject(CHANNEL_APP_OPTIONS)),
|
|
186
|
+
__param(1, Optional()),
|
|
187
|
+
__metadata("design:paramtypes", [Object, ExtensionDiscoveryService])
|
|
188
|
+
], ChannelAppService);
|
|
189
|
+
export { ChannelAppService };
|
|
190
|
+
//# sourceMappingURL=channel-app.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel-app.service.js","sourceRoot":"","sources":["../../src/nestjs/channel-app.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAqB,MAAM,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACzF,OAAO,EAKL,qBAAqB,GACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,GAGpB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,6CAA6C,CAAC;AAExF;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAE3B;IACA;IAFlB,YACkB,gBAAwB,EACxB,iBAA2B;QAE3C,KAAK,CACH,YAAY,gBAAgB,oCAAoC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC/F,CAAC;QALc,qBAAgB,GAAhB,gBAAgB,CAAQ;QACxB,sBAAiB,GAAjB,iBAAiB,CAAU;QAK3C,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAGM,IAAM,iB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;YACL,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK;SAC1B,CAAC;IACJ,CAAC;CACF,CAAA;AApLY,iBAAiB;IAD7B,UAAU,EAAE;IAKR,WAAA,MAAM,CAAC,mBAAmB,CAAC,CAAA;IAE3B,WAAA,QAAQ,EAAE,CAAA;6CACyB,yBAAyB;GAPpD,iBAAiB,CAoL7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nestjs/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nestjs/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { DynamicModule, Type, ForwardReference, InjectionToken } from "@nestjs/common";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for ChannelAppModule
|
|
4
|
+
*/
|
|
5
|
+
export interface ChannelAppModuleOptions {
|
|
6
|
+
/** Your app's ID from Channel.io */
|
|
7
|
+
appId: string;
|
|
8
|
+
/** Your app's secret from Channel.io */
|
|
9
|
+
appSecret: string;
|
|
10
|
+
/** Whether to enable debug logging (default: false) */
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Automatically register extensions with AppStore on module init
|
|
14
|
+
* When true, calls registerExtension API for each extension
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
autoRegister?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* AppStore API URL (default: https://app-store.channel.io)
|
|
20
|
+
* Only used when autoRegister is true
|
|
21
|
+
*/
|
|
22
|
+
appStoreUrl?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Callback called after auto-registration completes
|
|
25
|
+
* Useful for logging or error handling
|
|
26
|
+
*/
|
|
27
|
+
onAutoRegister?: (results: AutoRegisterResult[]) => void;
|
|
28
|
+
/**
|
|
29
|
+
* Signing key for verifying X-Signature header (hex-encoded)
|
|
30
|
+
* If not provided, appSecret will be used
|
|
31
|
+
* This should be the 64-character hex string from your app settings
|
|
32
|
+
*/
|
|
33
|
+
signingKey?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Skip signature verification (for development/testing only)
|
|
36
|
+
* WARNING: Never enable this in production!
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
skipSignatureVerification?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result of auto-registration for a single extension
|
|
43
|
+
*/
|
|
44
|
+
export interface AutoRegisterResult {
|
|
45
|
+
extensionName: string;
|
|
46
|
+
systemVersion: string;
|
|
47
|
+
success: boolean;
|
|
48
|
+
error?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Module import type for NestJS
|
|
52
|
+
*/
|
|
53
|
+
type ModuleImport = Type | DynamicModule | Promise<DynamicModule> | ForwardReference;
|
|
54
|
+
/**
|
|
55
|
+
* Async configuration options for ChannelAppModule
|
|
56
|
+
*/
|
|
57
|
+
export interface ChannelAppModuleAsyncOptions {
|
|
58
|
+
/** Factory function that returns module options */
|
|
59
|
+
useFactory: (...args: unknown[]) => Promise<ChannelAppModuleOptions> | ChannelAppModuleOptions;
|
|
60
|
+
/** Dependencies to inject into the factory */
|
|
61
|
+
inject?: InjectionToken[];
|
|
62
|
+
/** Modules to import */
|
|
63
|
+
imports?: ModuleImport[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Injection token for module options
|
|
67
|
+
*/
|
|
68
|
+
export declare const CHANNEL_APP_OPTIONS: unique symbol;
|
|
69
|
+
export {};
|
|
70
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/nestjs/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAE5F;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,oCAAoC;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,SAAS,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,kBAAkB,EAAE,KAAK,IAAI,CAAC;IACzD;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;CACrC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,KAAK,YAAY,GAAG,IAAI,GAAG,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,GAAG,gBAAgB,CAAC;AAErF;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C,mDAAmD;IACnD,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,uBAAuB,CAAC,GAAG,uBAAuB,CAAC;IAC/F,8CAA8C;IAC9C,MAAM,CAAC,EAAE,cAAc,EAAE,CAAC;IAC1B,wBAAwB;IACxB,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;CAC1B;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,eAAgC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/nestjs/types.ts"],"names":[],"mappings":"AAqEA;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type DynamicModule, type OnModuleInit } from "@nestjs/common";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import type { Context, FunctionCallRequest, FunctionCallResponse, GetFunctionsResponse } from "@channel.io/app-sdk-core";
|
|
4
|
+
interface SimpleFunctionDefinition {
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
inputSchema: z.ZodSchema;
|
|
8
|
+
inputJsonSchema: Record<string, unknown>;
|
|
9
|
+
handler: (ctx: Context, params: unknown) => Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
interface SimpleAppOptions {
|
|
12
|
+
appId: string;
|
|
13
|
+
appSecret: string;
|
|
14
|
+
autoRegister?: boolean;
|
|
15
|
+
appStoreUrl?: string;
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Simple MCP-like API for Channel.io apps
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const app = new ChannelApp({ appId: "...", appSecret: "..." })
|
|
24
|
+
* .function(
|
|
25
|
+
* "calendar.getAvailability",
|
|
26
|
+
* z.object({ startDate: z.string(), endDate: z.string() }),
|
|
27
|
+
* async (ctx, { startDate, endDate }) => {
|
|
28
|
+
* return { slots: [] };
|
|
29
|
+
* }
|
|
30
|
+
* )
|
|
31
|
+
* .function(
|
|
32
|
+
* "calendar.createBooking",
|
|
33
|
+
* z.object({ slotId: z.string() }),
|
|
34
|
+
* async (ctx, params) => {
|
|
35
|
+
* return { bookingId: "123" };
|
|
36
|
+
* }
|
|
37
|
+
* );
|
|
38
|
+
*
|
|
39
|
+
* // In app.module.ts
|
|
40
|
+
* @Module({
|
|
41
|
+
* imports: [app.toModule()],
|
|
42
|
+
* })
|
|
43
|
+
* export class AppModule {}
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class ChannelApp {
|
|
47
|
+
private functions;
|
|
48
|
+
private readonly options;
|
|
49
|
+
constructor(options: SimpleAppOptions);
|
|
50
|
+
/**
|
|
51
|
+
* Define a function (MCP-style)
|
|
52
|
+
*
|
|
53
|
+
* @param name Function name (e.g., "calendar.getAvailability")
|
|
54
|
+
* @param inputSchema Zod schema for input validation
|
|
55
|
+
* @param handler Function handler
|
|
56
|
+
* @param options Additional options
|
|
57
|
+
*/
|
|
58
|
+
function<TInput extends z.ZodSchema>(name: string, inputSchema: TInput, handler: (ctx: Context, params: z.infer<TInput>) => Promise<unknown>, options?: {
|
|
59
|
+
description?: string;
|
|
60
|
+
}): this;
|
|
61
|
+
/**
|
|
62
|
+
* Create NestJS module from the defined functions
|
|
63
|
+
*/
|
|
64
|
+
toModule(): DynamicModule;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Simple service for handling function calls
|
|
68
|
+
*/
|
|
69
|
+
declare class ChannelAppSimpleService implements OnModuleInit {
|
|
70
|
+
private readonly functions;
|
|
71
|
+
private readonly options;
|
|
72
|
+
private readonly logger;
|
|
73
|
+
constructor(functions: Map<string, SimpleFunctionDefinition>, options: SimpleAppOptions);
|
|
74
|
+
onModuleInit(): Promise<void>;
|
|
75
|
+
private autoRegister;
|
|
76
|
+
handleFunctionCall(request: FunctionCallRequest): Promise<FunctionCallResponse>;
|
|
77
|
+
getFunctions(): GetFunctionsResponse;
|
|
78
|
+
}
|
|
79
|
+
export { ChannelAppSimpleService };
|
|
80
|
+
//# sourceMappingURL=channel-app.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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,CAAC,EAAE,MAAM,CAAA;KAAE,GACjC,IAAI;IAyBP;;OAEG;IACH,QAAQ,IAAI,aAAa;CAa1B;AAED;;GAEG;AACH,cACM,uBAAwB,YAAW,YAAY;IAKjD,OAAO,CAAC,QAAQ,CAAC,SAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,OAAO;IAN1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA4C;gBAIhD,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,wBAAwB,CAAC,EAEhD,OAAO,EAAE,gBAAgB;IAGtC,YAAY;YAcJ,YAAY;IAqCpB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAyBrF,YAAY,IAAI,oBAAoB;CAwBrC;AA8CD,OAAO,EAAE,uBAAuB,EAAE,CAAC"}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
var ChannelAppSimpleService_1;
|
|
14
|
+
import { Module, Injectable, Inject, Logger, Controller, Post, Put, Body, Param, HttpException, HttpStatus, } from "@nestjs/common";
|
|
15
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
16
|
+
import { FunctionNotFoundError } from "@channel.io/app-sdk-core";
|
|
17
|
+
import { AppStoreClient } from "../appstore/client.js";
|
|
18
|
+
const SIMPLE_FUNCTIONS = Symbol("SIMPLE_FUNCTIONS");
|
|
19
|
+
const SIMPLE_OPTIONS = Symbol("SIMPLE_OPTIONS");
|
|
20
|
+
/**
|
|
21
|
+
* Simple MCP-like API for Channel.io apps
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const app = new ChannelApp({ appId: "...", appSecret: "..." })
|
|
26
|
+
* .function(
|
|
27
|
+
* "calendar.getAvailability",
|
|
28
|
+
* z.object({ startDate: z.string(), endDate: z.string() }),
|
|
29
|
+
* async (ctx, { startDate, endDate }) => {
|
|
30
|
+
* return { slots: [] };
|
|
31
|
+
* }
|
|
32
|
+
* )
|
|
33
|
+
* .function(
|
|
34
|
+
* "calendar.createBooking",
|
|
35
|
+
* z.object({ slotId: z.string() }),
|
|
36
|
+
* async (ctx, params) => {
|
|
37
|
+
* return { bookingId: "123" };
|
|
38
|
+
* }
|
|
39
|
+
* );
|
|
40
|
+
*
|
|
41
|
+
* // In app.module.ts
|
|
42
|
+
* @Module({
|
|
43
|
+
* imports: [app.toModule()],
|
|
44
|
+
* })
|
|
45
|
+
* export class AppModule {}
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export class ChannelApp {
|
|
49
|
+
functions = new Map();
|
|
50
|
+
options;
|
|
51
|
+
constructor(options) {
|
|
52
|
+
this.options = options;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Define a function (MCP-style)
|
|
56
|
+
*
|
|
57
|
+
* @param name Function name (e.g., "calendar.getAvailability")
|
|
58
|
+
* @param inputSchema Zod schema for input validation
|
|
59
|
+
* @param handler Function handler
|
|
60
|
+
* @param options Additional options
|
|
61
|
+
*/
|
|
62
|
+
function(name, inputSchema, handler, options) {
|
|
63
|
+
const inputJsonSchema = zodToJsonSchema(inputSchema, { $refStrategy: "none" });
|
|
64
|
+
const def = {
|
|
65
|
+
name,
|
|
66
|
+
inputSchema,
|
|
67
|
+
inputJsonSchema,
|
|
68
|
+
handler: async (ctx, params) => {
|
|
69
|
+
const validated = inputSchema.parse(params);
|
|
70
|
+
return handler(ctx, validated);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
if (options?.description) {
|
|
74
|
+
def.description = options.description;
|
|
75
|
+
}
|
|
76
|
+
this.functions.set(name, def);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Create NestJS module from the defined functions
|
|
81
|
+
*/
|
|
82
|
+
toModule() {
|
|
83
|
+
return {
|
|
84
|
+
module: ChannelAppSimpleModule,
|
|
85
|
+
providers: [
|
|
86
|
+
{ provide: SIMPLE_FUNCTIONS, useValue: this.functions },
|
|
87
|
+
{ provide: SIMPLE_OPTIONS, useValue: this.options },
|
|
88
|
+
ChannelAppSimpleService,
|
|
89
|
+
],
|
|
90
|
+
controllers: [ChannelAppSimpleController],
|
|
91
|
+
exports: [ChannelAppSimpleService],
|
|
92
|
+
global: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Simple service for handling function calls
|
|
98
|
+
*/
|
|
99
|
+
let ChannelAppSimpleService = ChannelAppSimpleService_1 = class ChannelAppSimpleService {
|
|
100
|
+
functions;
|
|
101
|
+
options;
|
|
102
|
+
logger = new Logger(ChannelAppSimpleService_1.name);
|
|
103
|
+
constructor(functions, options) {
|
|
104
|
+
this.functions = functions;
|
|
105
|
+
this.options = options;
|
|
106
|
+
}
|
|
107
|
+
async onModuleInit() {
|
|
108
|
+
this.logger.log(`Registered ${this.functions.size} function(s)`);
|
|
109
|
+
if (this.options.debug) {
|
|
110
|
+
for (const name of this.functions.keys()) {
|
|
111
|
+
this.logger.debug(` - ${name}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (this.options.autoRegister) {
|
|
115
|
+
await this.autoRegister();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async autoRegister() {
|
|
119
|
+
const client = new AppStoreClient({
|
|
120
|
+
appId: this.options.appId,
|
|
121
|
+
appSecret: this.options.appSecret,
|
|
122
|
+
appStoreUrl: this.options.appStoreUrl,
|
|
123
|
+
debug: this.options.debug,
|
|
124
|
+
});
|
|
125
|
+
// Extract unique extension names from function names
|
|
126
|
+
const extensions = new Set();
|
|
127
|
+
for (const name of this.functions.keys()) {
|
|
128
|
+
const [extensionName] = name.split(".");
|
|
129
|
+
if (extensionName) {
|
|
130
|
+
extensions.add(extensionName);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
for (const extensionName of extensions) {
|
|
134
|
+
try {
|
|
135
|
+
this.logger.log(`Auto-registering extension: ${extensionName}`);
|
|
136
|
+
const result = await client.registerExtension({
|
|
137
|
+
extensionName,
|
|
138
|
+
systemVersion: "v1",
|
|
139
|
+
});
|
|
140
|
+
if (result.success) {
|
|
141
|
+
this.logger.log(`Successfully registered extension: ${extensionName}`);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const errorMsg = result.error_message ?? "Unknown error";
|
|
145
|
+
this.logger.warn(`Failed to register extension ${extensionName}: ${errorMsg}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
this.logger.error(`Error registering extension ${extensionName}: ${String(error)}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async handleFunctionCall(request) {
|
|
154
|
+
const { method, context, params } = request;
|
|
155
|
+
// Handle getFunctions
|
|
156
|
+
if (method === "extension.core.function.getFunctions") {
|
|
157
|
+
return this.getFunctions();
|
|
158
|
+
}
|
|
159
|
+
// Find function - support both "extension.name.func" and direct "name.func" format
|
|
160
|
+
let func = this.functions.get(method);
|
|
161
|
+
// Try stripping "extension." prefix
|
|
162
|
+
if (!func && method.startsWith("extension.")) {
|
|
163
|
+
const withoutPrefix = method.slice("extension.".length);
|
|
164
|
+
func = this.functions.get(withoutPrefix);
|
|
165
|
+
}
|
|
166
|
+
if (!func) {
|
|
167
|
+
throw new FunctionNotFoundError(method);
|
|
168
|
+
}
|
|
169
|
+
const result = await func.handler(context, params ?? {});
|
|
170
|
+
return { result };
|
|
171
|
+
}
|
|
172
|
+
getFunctions() {
|
|
173
|
+
const functions = [];
|
|
174
|
+
for (const func of this.functions.values()) {
|
|
175
|
+
const schema = {
|
|
176
|
+
name: `extension.${func.name}`,
|
|
177
|
+
inputSchema: func.inputJsonSchema,
|
|
178
|
+
};
|
|
179
|
+
if (func.description) {
|
|
180
|
+
schema.description = func.description;
|
|
181
|
+
}
|
|
182
|
+
functions.push(schema);
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
result: {
|
|
186
|
+
functions,
|
|
187
|
+
success: true,
|
|
188
|
+
errorMessage: "",
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
ChannelAppSimpleService = ChannelAppSimpleService_1 = __decorate([
|
|
194
|
+
Injectable(),
|
|
195
|
+
__param(0, Inject(SIMPLE_FUNCTIONS)),
|
|
196
|
+
__param(1, Inject(SIMPLE_OPTIONS)),
|
|
197
|
+
__metadata("design:paramtypes", [Map, Object])
|
|
198
|
+
], ChannelAppSimpleService);
|
|
199
|
+
/**
|
|
200
|
+
* Simple controller for handling function calls
|
|
201
|
+
*/
|
|
202
|
+
let ChannelAppSimpleController = class ChannelAppSimpleController {
|
|
203
|
+
service;
|
|
204
|
+
constructor(service) {
|
|
205
|
+
this.service = service;
|
|
206
|
+
}
|
|
207
|
+
async handleVersionedFunctions(_version, body) {
|
|
208
|
+
return this.handleRequest(body);
|
|
209
|
+
}
|
|
210
|
+
async handleFunctions(body) {
|
|
211
|
+
return this.handleRequest(body);
|
|
212
|
+
}
|
|
213
|
+
async handleRequest(body) {
|
|
214
|
+
try {
|
|
215
|
+
return await this.service.handleFunctionCall(body);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (error instanceof FunctionNotFoundError) {
|
|
219
|
+
throw new HttpException({ error: "FUNCTION_NOT_FOUND", message: error.message }, HttpStatus.NOT_FOUND);
|
|
220
|
+
}
|
|
221
|
+
throw new HttpException({
|
|
222
|
+
error: "INTERNAL_ERROR",
|
|
223
|
+
message: error instanceof Error ? error.message : "Unknown error",
|
|
224
|
+
}, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
__decorate([
|
|
229
|
+
Put(":version"),
|
|
230
|
+
__param(0, Param("version")),
|
|
231
|
+
__param(1, Body()),
|
|
232
|
+
__metadata("design:type", Function),
|
|
233
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
234
|
+
__metadata("design:returntype", Promise)
|
|
235
|
+
], ChannelAppSimpleController.prototype, "handleVersionedFunctions", null);
|
|
236
|
+
__decorate([
|
|
237
|
+
Post(),
|
|
238
|
+
__param(0, Body()),
|
|
239
|
+
__metadata("design:type", Function),
|
|
240
|
+
__metadata("design:paramtypes", [Object]),
|
|
241
|
+
__metadata("design:returntype", Promise)
|
|
242
|
+
], ChannelAppSimpleController.prototype, "handleFunctions", null);
|
|
243
|
+
ChannelAppSimpleController = __decorate([
|
|
244
|
+
Controller("functions"),
|
|
245
|
+
__metadata("design:paramtypes", [ChannelAppSimpleService])
|
|
246
|
+
], ChannelAppSimpleController);
|
|
247
|
+
let ChannelAppSimpleModule = class ChannelAppSimpleModule {
|
|
248
|
+
};
|
|
249
|
+
ChannelAppSimpleModule = __decorate([
|
|
250
|
+
Module({})
|
|
251
|
+
], ChannelAppSimpleModule);
|
|
252
|
+
export { ChannelAppSimpleService };
|
|
253
|
+
//# sourceMappingURL=channel-app.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|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