@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,110 @@
|
|
|
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
|
+
import { Injectable } from "@nestjs/common";
|
|
8
|
+
/**
|
|
9
|
+
* Middleware to capture raw request body for signature verification
|
|
10
|
+
*
|
|
11
|
+
* This middleware reads the raw body and stores it in `request.rawBody`
|
|
12
|
+
* while still allowing the body to be parsed by subsequent middlewares.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Apply in NestJS module
|
|
17
|
+
* export class AppModule implements NestModule {
|
|
18
|
+
* configure(consumer: MiddlewareConsumer) {
|
|
19
|
+
* consumer
|
|
20
|
+
* .apply(SignatureMiddleware)
|
|
21
|
+
* .forRoutes('functions');
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Or configure globally in main.ts:
|
|
27
|
+
* ```typescript
|
|
28
|
+
* // main.ts - Alternative approach with express
|
|
29
|
+
* import express from 'express';
|
|
30
|
+
*
|
|
31
|
+
* app.use(express.json({
|
|
32
|
+
* verify: (req, res, buf) => {
|
|
33
|
+
* req.rawBody = buf;
|
|
34
|
+
* },
|
|
35
|
+
* }));
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
let SignatureMiddleware = class SignatureMiddleware {
|
|
39
|
+
use(req, _res, next) {
|
|
40
|
+
// If rawBody already exists (set by another middleware), skip
|
|
41
|
+
if (req.rawBody) {
|
|
42
|
+
next();
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const chunks = [];
|
|
46
|
+
req.on("data", (chunk) => {
|
|
47
|
+
chunks.push(chunk);
|
|
48
|
+
});
|
|
49
|
+
req.on("end", () => {
|
|
50
|
+
if (chunks.length > 0) {
|
|
51
|
+
req.rawBody = Buffer.concat(chunks);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
next();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
SignatureMiddleware = __decorate([
|
|
58
|
+
Injectable()
|
|
59
|
+
], SignatureMiddleware);
|
|
60
|
+
export { SignatureMiddleware };
|
|
61
|
+
/**
|
|
62
|
+
* Express middleware function for raw body capture
|
|
63
|
+
* Use this if you prefer a function-based middleware
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* // main.ts
|
|
68
|
+
* app.use('/functions', rawBodyMiddleware);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function rawBodyMiddleware(req, _res, next) {
|
|
72
|
+
if (req.rawBody) {
|
|
73
|
+
next();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const chunks = [];
|
|
77
|
+
req.on("data", (chunk) => {
|
|
78
|
+
chunks.push(chunk);
|
|
79
|
+
});
|
|
80
|
+
req.on("end", () => {
|
|
81
|
+
if (chunks.length > 0) {
|
|
82
|
+
req.rawBody = Buffer.concat(chunks);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
next();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create express.json() options with raw body capture
|
|
89
|
+
* This is the recommended approach for signature verification
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* // main.ts
|
|
94
|
+
* import express from 'express';
|
|
95
|
+
*
|
|
96
|
+
* const app = await NestFactory.create(AppModule, {
|
|
97
|
+
* bodyParser: false, // Disable default body parser
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* app.use(express.json(jsonWithRawBody()));
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function jsonWithRawBody() {
|
|
104
|
+
return {
|
|
105
|
+
verify: (req, _res, buf) => {
|
|
106
|
+
req.rawBody = buf;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=signature.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.middleware.js","sourceRoot":"","sources":["../../src/guards/signature.middleware.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,UAAU,EAAuB,MAAM,gBAAgB,CAAC;AAcjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEI,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAC9B,GAAG,CAAC,GAAY,EAAE,IAAc,EAAE,IAAkB;QAClD,8DAA8D;QAC9D,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YAChB,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAa,EAAE,CAAC;QAE5B,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE;YAC/B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC,CAAC,CAAC;QAEH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;YACjB,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACtB,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACtC,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,IAAI,EAAE,CAAC;IACT,CAAC;CACF,CAAA;AAtBY,mBAAmB;IAD/B,UAAU,EAAE;GACA,mBAAmB,CAsB/B;;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAY,EAAE,IAAc,EAAE,IAAkB;IAChF,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;QAChB,IAAI,EAAE,CAAC;QACP,OAAO;IACT,CAAC;IAED,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE;QAC/B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;QACjB,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACtB,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,IAAI,EAAE,CAAC;AACT,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,eAAe;IAG7B,OAAO;QACL,MAAM,EAAE,CAAC,GAAY,EAAE,IAAc,EAAE,GAAW,EAAE,EAAE;YACpD,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC;QACpB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "@channel.io/app-sdk-core";
|
|
2
|
+
export * from "./nestjs/index.js";
|
|
3
|
+
export * from "./decorators/index.js";
|
|
4
|
+
export * from "./discovery/index.js";
|
|
5
|
+
export * from "./token/index.js";
|
|
6
|
+
export * from "./appstore/index.js";
|
|
7
|
+
export * from "./guards/index.js";
|
|
8
|
+
export * from "./native/index.js";
|
|
9
|
+
export * from "./simple/index.js";
|
|
10
|
+
export * from "./testing/index.js";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,0BAA0B,CAAC;AAGzC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,uBAAuB,CAAC;AAGtC,cAAc,sBAAsB,CAAC;AAGrC,cAAc,kBAAkB,CAAC;AAGjC,cAAc,qBAAqB,CAAC;AAGpC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Re-export core types and utilities
|
|
2
|
+
export * from "@channel.io/app-sdk-core";
|
|
3
|
+
// Export NestJS module and service
|
|
4
|
+
export * from "./nestjs/index.js";
|
|
5
|
+
// Export decorators
|
|
6
|
+
export * from "./decorators/index.js";
|
|
7
|
+
// Export discovery service
|
|
8
|
+
export * from "./discovery/index.js";
|
|
9
|
+
// Export token management utilities
|
|
10
|
+
export * from "./token/index.js";
|
|
11
|
+
// Export AppStore client utilities
|
|
12
|
+
export * from "./appstore/index.js";
|
|
13
|
+
// Export guards and middleware
|
|
14
|
+
export * from "./guards/index.js";
|
|
15
|
+
// Export native function client
|
|
16
|
+
export * from "./native/index.js";
|
|
17
|
+
// Export simple MCP-like API
|
|
18
|
+
export * from "./simple/index.js";
|
|
19
|
+
// Export testing utilities
|
|
20
|
+
export * from "./testing/index.js";
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,cAAc,0BAA0B,CAAC;AAEzC,mCAAmC;AACnC,cAAc,mBAAmB,CAAC;AAElC,oBAAoB;AACpB,cAAc,uBAAuB,CAAC;AAEtC,2BAA2B;AAC3B,cAAc,sBAAsB,CAAC;AAErC,oCAAoC;AACpC,cAAc,kBAAkB,CAAC;AAEjC,mCAAmC;AACnC,cAAc,qBAAqB,CAAC;AAEpC,+BAA+B;AAC/B,cAAc,mBAAmB,CAAC;AAElC,gCAAgC;AAChC,cAAc,mBAAmB,CAAC;AAElC,6BAA6B;AAC7B,cAAc,mBAAmB,CAAC;AAElC,2BAA2B;AAC3B,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { NativeFunctionClientConfig, FunctionContext, IssueTokenResult, RegisterCommandsResult, CommandDTO } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Native Function Client for ch-app-store
|
|
4
|
+
*
|
|
5
|
+
* Allows app servers to call PUBLIC native functions provided by ch-app-store.
|
|
6
|
+
* Public functions don't require special RBAC claims.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { NativeFunctionClient } from '@channel.io/app-sdk-server';
|
|
11
|
+
*
|
|
12
|
+
* const client = new NativeFunctionClient({
|
|
13
|
+
* appId: process.env.APP_ID,
|
|
14
|
+
* appSecret: process.env.APP_SECRET,
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Issue a token
|
|
18
|
+
* const token = await client.issueToken({ channelId: 'channel-123' });
|
|
19
|
+
*
|
|
20
|
+
* // Register commands
|
|
21
|
+
* await client.registerCommands([
|
|
22
|
+
* {
|
|
23
|
+
* name: 'hello',
|
|
24
|
+
* scope: 'desk',
|
|
25
|
+
* actionFunctionName: 'hello.action',
|
|
26
|
+
* alfMode: 'disable',
|
|
27
|
+
* },
|
|
28
|
+
* ]);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class NativeFunctionClient {
|
|
32
|
+
private readonly config;
|
|
33
|
+
private readonly debug;
|
|
34
|
+
constructor(config: NativeFunctionClientConfig);
|
|
35
|
+
/**
|
|
36
|
+
* Issue an access token
|
|
37
|
+
*
|
|
38
|
+
* @param options Token issuance options (channelId is optional)
|
|
39
|
+
* @returns Token response
|
|
40
|
+
*/
|
|
41
|
+
issueToken(options?: {
|
|
42
|
+
channelId?: string;
|
|
43
|
+
}): Promise<IssueTokenResult>;
|
|
44
|
+
/**
|
|
45
|
+
* Refresh an access token
|
|
46
|
+
*
|
|
47
|
+
* @param refreshToken The refresh token
|
|
48
|
+
* @returns New token response
|
|
49
|
+
*/
|
|
50
|
+
refreshToken(refreshToken: string): Promise<IssueTokenResult>;
|
|
51
|
+
/**
|
|
52
|
+
* Register slash commands for the app
|
|
53
|
+
*
|
|
54
|
+
* @param commands Commands to register (max 30)
|
|
55
|
+
* @returns Registration result
|
|
56
|
+
*/
|
|
57
|
+
registerCommands(commands: CommandDTO[]): Promise<RegisterCommandsResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Call any native function (no authentication required for public functions)
|
|
60
|
+
*
|
|
61
|
+
* @param method Function method name
|
|
62
|
+
* @param params Function params
|
|
63
|
+
* @returns Function result
|
|
64
|
+
*/
|
|
65
|
+
callNativeFunction<TParams, TResult>(method: string, params?: TParams): Promise<TResult>;
|
|
66
|
+
/**
|
|
67
|
+
* Call a native function with authentication token
|
|
68
|
+
* Use this for private native functions that require RBAC claims
|
|
69
|
+
*
|
|
70
|
+
* @param method Function method name
|
|
71
|
+
* @param params Function params
|
|
72
|
+
* @param accessToken Access token
|
|
73
|
+
* @returns Function result
|
|
74
|
+
*/
|
|
75
|
+
callNativeFunctionWithToken<TParams, TResult>(method: string, params: TParams, accessToken: string): Promise<TResult>;
|
|
76
|
+
/**
|
|
77
|
+
* Call an app function (function provided by another app or your own app)
|
|
78
|
+
*
|
|
79
|
+
* @param appId Target app ID
|
|
80
|
+
* @param method Function method name
|
|
81
|
+
* @param params Function params
|
|
82
|
+
* @param context Function context
|
|
83
|
+
* @param accessToken Access token
|
|
84
|
+
* @param systemVersion System version (optional)
|
|
85
|
+
* @returns Function result
|
|
86
|
+
*/
|
|
87
|
+
callAppFunction<TParams, TResult>(appId: string, method: string, params: TParams, context: FunctionContext, accessToken: string, systemVersion?: string): Promise<TResult>;
|
|
88
|
+
/**
|
|
89
|
+
* Handle API response
|
|
90
|
+
*/
|
|
91
|
+
private handleResponse;
|
|
92
|
+
/**
|
|
93
|
+
* Debug logging
|
|
94
|
+
*/
|
|
95
|
+
private log;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Native function error
|
|
99
|
+
*/
|
|
100
|
+
export declare class NativeFunctionError extends Error {
|
|
101
|
+
readonly code?: number | undefined;
|
|
102
|
+
readonly responseBody?: string | undefined;
|
|
103
|
+
constructor(message: string, code?: number | undefined, responseBody?: string | undefined);
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/native/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAG1B,eAAe,EAEf,gBAAgB,EAGhB,sBAAsB,EACtB,UAAU,EACX,MAAM,YAAY,CAAC;AAOpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAGM;IAC7B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAU;gBAEpB,MAAM,EAAE,0BAA0B;IAY9C;;;;;OAKG;IACH,UAAU,CAAC,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC;IASvE;;;;;OAKG;IACH,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAU7D;;;;;OAKG;IACH,gBAAgB,CAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAezE;;;;;;OAMG;IACG,kBAAkB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAqB9F;;;;;;;;OAQG;IACG,2BAA2B,CAAC,OAAO,EAAE,OAAO,EAChD,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,OAAO,EACf,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,OAAO,CAAC;IAsBnB;;;;;;;;;;OAUG;IACG,eAAe,CAAC,OAAO,EAAE,OAAO,EACpC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,eAAe,EACxB,WAAW,EAAE,MAAM,EACnB,aAAa,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,OAAO,CAAC;IAwBnB;;OAEG;YACW,cAAc;IAyB5B;;OAEG;IACH,OAAO,CAAC,GAAG;CASZ;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aAG1B,IAAI,CAAC,EAAE,MAAM;aACb,YAAY,CAAC,EAAE,MAAM;gBAFrC,OAAO,EAAE,MAAM,EACC,IAAI,CAAC,EAAE,MAAM,YAAA,EACb,YAAY,CAAC,EAAE,MAAM,YAAA;CAKxC"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default AppStore API URL
|
|
3
|
+
*/
|
|
4
|
+
const DEFAULT_APPSTORE_URL = "https://app-store.channel.io";
|
|
5
|
+
/**
|
|
6
|
+
* Native Function Client for ch-app-store
|
|
7
|
+
*
|
|
8
|
+
* Allows app servers to call PUBLIC native functions provided by ch-app-store.
|
|
9
|
+
* Public functions don't require special RBAC claims.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { NativeFunctionClient } from '@channel.io/app-sdk-server';
|
|
14
|
+
*
|
|
15
|
+
* const client = new NativeFunctionClient({
|
|
16
|
+
* appId: process.env.APP_ID,
|
|
17
|
+
* appSecret: process.env.APP_SECRET,
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // Issue a token
|
|
21
|
+
* const token = await client.issueToken({ channelId: 'channel-123' });
|
|
22
|
+
*
|
|
23
|
+
* // Register commands
|
|
24
|
+
* await client.registerCommands([
|
|
25
|
+
* {
|
|
26
|
+
* name: 'hello',
|
|
27
|
+
* scope: 'desk',
|
|
28
|
+
* actionFunctionName: 'hello.action',
|
|
29
|
+
* alfMode: 'disable',
|
|
30
|
+
* },
|
|
31
|
+
* ]);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export class NativeFunctionClient {
|
|
35
|
+
config;
|
|
36
|
+
debug;
|
|
37
|
+
constructor(config) {
|
|
38
|
+
this.config = {
|
|
39
|
+
...config,
|
|
40
|
+
appStoreUrl: config.appStoreUrl ?? DEFAULT_APPSTORE_URL,
|
|
41
|
+
};
|
|
42
|
+
this.debug = config.debug ?? false;
|
|
43
|
+
}
|
|
44
|
+
// ============================================
|
|
45
|
+
// Auth Functions (PUBLIC)
|
|
46
|
+
// ============================================
|
|
47
|
+
/**
|
|
48
|
+
* Issue an access token
|
|
49
|
+
*
|
|
50
|
+
* @param options Token issuance options (channelId is optional)
|
|
51
|
+
* @returns Token response
|
|
52
|
+
*/
|
|
53
|
+
issueToken(options) {
|
|
54
|
+
const params = {
|
|
55
|
+
secret: this.config.appSecret,
|
|
56
|
+
...(options?.channelId && { channelId: options.channelId }),
|
|
57
|
+
};
|
|
58
|
+
return this.callNativeFunction("issueToken", params);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Refresh an access token
|
|
62
|
+
*
|
|
63
|
+
* @param refreshToken The refresh token
|
|
64
|
+
* @returns New token response
|
|
65
|
+
*/
|
|
66
|
+
refreshToken(refreshToken) {
|
|
67
|
+
const params = { refreshToken };
|
|
68
|
+
return this.callNativeFunction("refreshToken", params);
|
|
69
|
+
}
|
|
70
|
+
// ============================================
|
|
71
|
+
// Command Functions (PUBLIC)
|
|
72
|
+
// ============================================
|
|
73
|
+
/**
|
|
74
|
+
* Register slash commands for the app
|
|
75
|
+
*
|
|
76
|
+
* @param commands Commands to register (max 30)
|
|
77
|
+
* @returns Registration result
|
|
78
|
+
*/
|
|
79
|
+
registerCommands(commands) {
|
|
80
|
+
const params = {
|
|
81
|
+
appId: this.config.appId,
|
|
82
|
+
commands,
|
|
83
|
+
};
|
|
84
|
+
return this.callNativeFunction("registerCommands", params);
|
|
85
|
+
}
|
|
86
|
+
// ============================================
|
|
87
|
+
// Generic Function Calls
|
|
88
|
+
// ============================================
|
|
89
|
+
/**
|
|
90
|
+
* Call any native function (no authentication required for public functions)
|
|
91
|
+
*
|
|
92
|
+
* @param method Function method name
|
|
93
|
+
* @param params Function params
|
|
94
|
+
* @returns Function result
|
|
95
|
+
*/
|
|
96
|
+
async callNativeFunction(method, params) {
|
|
97
|
+
const url = `${this.config.appStoreUrl}/general/v1/native/functions`;
|
|
98
|
+
const request = {
|
|
99
|
+
method,
|
|
100
|
+
...(params && { params }),
|
|
101
|
+
};
|
|
102
|
+
this.log(`Calling native function: ${method}`, params);
|
|
103
|
+
const response = await fetch(url, {
|
|
104
|
+
method: "PUT",
|
|
105
|
+
headers: {
|
|
106
|
+
"Content-Type": "application/json",
|
|
107
|
+
},
|
|
108
|
+
body: JSON.stringify(request),
|
|
109
|
+
});
|
|
110
|
+
return this.handleResponse(response, method);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Call a native function with authentication token
|
|
114
|
+
* Use this for private native functions that require RBAC claims
|
|
115
|
+
*
|
|
116
|
+
* @param method Function method name
|
|
117
|
+
* @param params Function params
|
|
118
|
+
* @param accessToken Access token
|
|
119
|
+
* @returns Function result
|
|
120
|
+
*/
|
|
121
|
+
async callNativeFunctionWithToken(method, params, accessToken) {
|
|
122
|
+
const url = `${this.config.appStoreUrl}/general/v1/native/functions`;
|
|
123
|
+
const request = {
|
|
124
|
+
method,
|
|
125
|
+
params,
|
|
126
|
+
};
|
|
127
|
+
this.log(`Calling native function with token: ${method}`, params);
|
|
128
|
+
const response = await fetch(url, {
|
|
129
|
+
method: "PUT",
|
|
130
|
+
headers: {
|
|
131
|
+
"Content-Type": "application/json",
|
|
132
|
+
"x-access-token": accessToken,
|
|
133
|
+
},
|
|
134
|
+
body: JSON.stringify(request),
|
|
135
|
+
});
|
|
136
|
+
return this.handleResponse(response, method);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Call an app function (function provided by another app or your own app)
|
|
140
|
+
*
|
|
141
|
+
* @param appId Target app ID
|
|
142
|
+
* @param method Function method name
|
|
143
|
+
* @param params Function params
|
|
144
|
+
* @param context Function context
|
|
145
|
+
* @param accessToken Access token
|
|
146
|
+
* @param systemVersion System version (optional)
|
|
147
|
+
* @returns Function result
|
|
148
|
+
*/
|
|
149
|
+
async callAppFunction(appId, method, params, context, accessToken, systemVersion) {
|
|
150
|
+
const url = `${this.config.appStoreUrl}/general/v1/apps/${appId}/functions`;
|
|
151
|
+
const request = {
|
|
152
|
+
method,
|
|
153
|
+
params,
|
|
154
|
+
context,
|
|
155
|
+
...(systemVersion && { systemVersion }),
|
|
156
|
+
};
|
|
157
|
+
this.log(`Calling app function: ${appId}/${method}`, { params, context });
|
|
158
|
+
const response = await fetch(url, {
|
|
159
|
+
method: "PUT",
|
|
160
|
+
headers: {
|
|
161
|
+
"Content-Type": "application/json",
|
|
162
|
+
"x-access-token": accessToken,
|
|
163
|
+
},
|
|
164
|
+
body: JSON.stringify(request),
|
|
165
|
+
});
|
|
166
|
+
return this.handleResponse(response, method);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Handle API response
|
|
170
|
+
*/
|
|
171
|
+
async handleResponse(response, method) {
|
|
172
|
+
if (!response.ok) {
|
|
173
|
+
const errorBody = await response.text();
|
|
174
|
+
throw new NativeFunctionError(`Native function call failed: ${response.status} ${response.statusText}`, response.status, errorBody);
|
|
175
|
+
}
|
|
176
|
+
const jsonResponse = (await response.json());
|
|
177
|
+
if (jsonResponse.error) {
|
|
178
|
+
throw new NativeFunctionError(jsonResponse.error.message, jsonResponse.error.code, JSON.stringify(jsonResponse.error.data));
|
|
179
|
+
}
|
|
180
|
+
this.log(`${method} response`, jsonResponse.result);
|
|
181
|
+
return jsonResponse.result;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Debug logging
|
|
185
|
+
*/
|
|
186
|
+
log(message, data) {
|
|
187
|
+
if (this.debug) {
|
|
188
|
+
if (data !== undefined) {
|
|
189
|
+
console.debug(`[NativeFunctionClient] ${message}`, data);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
console.debug(`[NativeFunctionClient] ${message}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Native function error
|
|
199
|
+
*/
|
|
200
|
+
export class NativeFunctionError extends Error {
|
|
201
|
+
code;
|
|
202
|
+
responseBody;
|
|
203
|
+
constructor(message, code, responseBody) {
|
|
204
|
+
super(message);
|
|
205
|
+
this.code = code;
|
|
206
|
+
this.responseBody = responseBody;
|
|
207
|
+
this.name = "NativeFunctionError";
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/native/client.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,MAAM,oBAAoB,GAAG,8BAA8B,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,OAAO,oBAAoB;IACd,MAAM,CAGM;IACZ,KAAK,CAAU;IAEhC,YAAY,MAAkC;QAC5C,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,MAAM;YACT,W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OAEG;IACK,GAAG,CAAC,OAAe,EAAE,IAAc;QACzC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACvB,OAAO,CAAC,KAAK,CAAC,0BAA0B,OAAO,EAAE,EAAE,IAAI,CAAC,CAAC;YAC3D,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,0BAA0B,OAAO,EAAE,CAAC,CAAC;YACrD,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAG1B;IACA;IAHlB,YACE,OAAe,EACC,IAAa,EACb,YAAqB;QAErC,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,SAAI,GAAJ,IAAI,CAAS;QACb,iBAAY,GAAZ,YAAY,CAAS;QAGrC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/native/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/native/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
|