Skip to content

Commit a6eb5b6

Browse files
committed
🐛 fix: close ajv strict mode
1 parent 1f38b3a commit a6eb5b6

File tree

2 files changed

+133
-53
lines changed

2 files changed

+133
-53
lines changed

src/node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const createGatewayOnNodeRuntime = (options: NodeRuntimeGatewayOptions =
1515
const gateway = new Gateway({
1616
...options,
1717
Validator: (schema, value) => {
18-
const ajv = new Ajv({ validateFormats: false });
18+
const ajv = new Ajv({ strict: false });
1919
const validate = ajv.compile(schema);
2020

2121
const valid = validate(value);
@@ -48,10 +48,10 @@ export const createGatewayOnNodeRuntime = (options: NodeRuntimeGatewayOptions =
4848

4949
res.send(data);
5050
} catch (error) {
51-
console.log(error);
51+
console.error(error);
5252
const { errorType, body } = error as GatewayErrorResponse;
5353

54-
res.status(getPluginErrorStatus(errorType)).send({ body, errorType });
54+
res.status(getPluginErrorStatus(errorType)).send(errorType ? { body, errorType } : error);
5555
}
5656
};
5757
};

tests/gateway.test.ts

Lines changed: 130 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@ describe('Gateway', () => {
1616
expect(gateway['pluginIndexUrl']).toBe('https://test-market-index-url.com');
1717
});
1818

19-
it('run with ajv validator', async () => {
20-
const mockResult = JSON.stringify({ success: true });
21-
vi.mocked(SwaggerClient).mockResolvedValue({
22-
execute: vi.fn().mockResolvedValue({
23-
status: 200,
24-
text: mockResult,
25-
}),
26-
});
27-
19+
describe('using AJV Validator', () => {
2820
const gateway = new Gateway({
2921
Validator: (schema, value) => {
30-
const ajv = new Ajv({ validateFormats: false });
22+
const ajv = new Ajv({ strict: false });
3123
const validate = ajv.compile(schema);
3224

3325
const valid = validate(value);
@@ -38,53 +30,141 @@ describe('Gateway', () => {
3830
},
3931
});
4032

41-
const payload = {
42-
apiName: 'getSupportedVendors',
43-
arguments: '{}',
44-
identifier: 'mock-credit-card',
45-
manifest: {
46-
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
47-
api: [
48-
{
49-
description: 'It is API to get list of supported vendors',
50-
name: 'getSupportedVendors',
51-
parameters: {
52-
properties: {},
53-
type: 'object',
33+
it('run with ajv validator', async () => {
34+
const mockResult = JSON.stringify({ success: true });
35+
vi.mocked(SwaggerClient).mockResolvedValue({
36+
execute: vi.fn().mockResolvedValue({
37+
status: 200,
38+
text: mockResult,
39+
}),
40+
});
41+
42+
const payload = {
43+
apiName: 'getSupportedVendors',
44+
arguments: '{}',
45+
identifier: 'mock-credit-card',
46+
manifest: {
47+
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
48+
api: [
49+
{
50+
description: 'It is API to get list of supported vendors',
51+
name: 'getSupportedVendors',
52+
parameters: {
53+
properties: {},
54+
type: 'object',
55+
},
5456
},
57+
],
58+
author: 'arvinxx',
59+
createdAt: '2023-12-11',
60+
identifier: 'mock-credit-card',
61+
meta: {
62+
avatar: '💳',
63+
description: 'Credit Card Number Generator',
64+
tags: ['credit-card', 'mockup', 'generator'],
65+
title: 'Credit Card Generator',
5566
},
56-
],
57-
author: 'arvinxx',
58-
createdAt: '2023-12-11',
59-
identifier: 'mock-credit-card',
60-
meta: {
61-
avatar: '💳',
62-
description: 'Credit Card Number Generator',
63-
tags: ['credit-card', 'mockup', 'generator'],
64-
title: 'Credit Card Generator',
67+
openapi:
68+
'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json',
69+
settings: {
70+
properties: {
71+
apiKeyAuth: {
72+
description: 'apiKeyAuth API Key',
73+
format: 'password',
74+
title: 'X-OpenAPIHub-Key',
75+
type: 'string',
76+
},
77+
},
78+
required: ['apiKeyAuth'],
79+
type: 'object',
80+
},
81+
version: '1',
6582
},
66-
openapi:
67-
'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json',
68-
settings: {
69-
properties: {
70-
apiKeyAuth: {
71-
description: 'apiKeyAuth API Key',
72-
format: 'password',
73-
title: 'X-OpenAPIHub-Key',
74-
type: 'string',
83+
type: 'default',
84+
} as PluginRequestPayload;
85+
86+
const res = await gateway.execute(payload, { apiKeyAuth: 'abc' });
87+
88+
expect(res.success).toBeTruthy();
89+
expect(res.data).toEqual(mockResult);
90+
});
91+
92+
it('run with ajv with $$refs', async () => {
93+
const mockResult = JSON.stringify({ success: true });
94+
vi.mocked(SwaggerClient).mockResolvedValue({
95+
execute: vi.fn().mockResolvedValue({
96+
status: 200,
97+
text: mockResult,
98+
}),
99+
});
100+
101+
const payload = {
102+
apiName: 'getRandomCardNumber',
103+
arguments: '{"vendor":"visa"}',
104+
identifier: 'mock-credit-card',
105+
manifest: {
106+
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
107+
api: [
108+
{
109+
description: 'It is API to generate random credit card numbers',
110+
name: 'getRandomCardNumber',
111+
parameters: {
112+
properties: {
113+
vendor: {
114+
$$ref: 'https://chat-dev.lobehub.com/chat#/components/schemas/Vendor',
115+
enum: [
116+
'visa',
117+
'master-card',
118+
'diners-club',
119+
'american-express',
120+
'discover',
121+
'jcb',
122+
],
123+
type: 'string',
124+
},
125+
},
126+
required: ['vendor'],
127+
type: 'object',
128+
},
129+
},
130+
{
131+
description: 'It is API to get list of supported vendors',
132+
name: 'getSupportedVendors',
133+
parameters: { properties: {}, type: 'object' },
75134
},
135+
],
136+
author: 'arvinxx',
137+
createdAt: '2023-12-11',
138+
identifier: 'mock-credit-card',
139+
meta: {
140+
avatar: '💳',
141+
description: 'Credit Card Number Generator',
142+
tags: ['credit-card', 'mockup', 'generator'],
143+
title: 'Credit Card Generator',
76144
},
77-
required: ['apiKeyAuth'],
78-
type: 'object',
145+
openapi:
146+
'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json',
147+
settings: {
148+
properties: {
149+
apiKeyAuth: {
150+
description: 'apiKeyAuth API Key',
151+
format: 'password',
152+
title: 'X-OpenAPIHub-Key',
153+
type: 'string',
154+
},
155+
},
156+
required: ['apiKeyAuth'],
157+
type: 'object',
158+
},
159+
version: '1',
79160
},
80-
version: '1',
81-
},
82-
type: 'default',
83-
} as PluginRequestPayload;
161+
type: 'default',
162+
} as PluginRequestPayload;
84163

85-
const res = await gateway.execute(payload, { apiKeyAuth: 'abc' });
164+
const res = await gateway.execute(payload, { apiKeyAuth: 'abc' });
86165

87-
expect(res.success).toBeTruthy();
88-
expect(res.data).toEqual(mockResult);
166+
expect(res.success).toBeTruthy();
167+
expect(res.data).toEqual(mockResult);
168+
});
89169
});
90170
});

0 commit comments

Comments
 (0)