Skip to content

Commit

Permalink
馃悰 fix: close ajv strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 13, 2023
1 parent 1f38b3a commit a6eb5b6
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const createGatewayOnNodeRuntime = (options: NodeRuntimeGatewayOptions =
const gateway = new Gateway({
...options,
Validator: (schema, value) => {
const ajv = new Ajv({ validateFormats: false });
const ajv = new Ajv({ strict: false });
const validate = ajv.compile(schema);

const valid = validate(value);
Expand Down Expand Up @@ -48,10 +48,10 @@ export const createGatewayOnNodeRuntime = (options: NodeRuntimeGatewayOptions =

res.send(data);
} catch (error) {
console.log(error);
console.error(error);
const { errorType, body } = error as GatewayErrorResponse;

res.status(getPluginErrorStatus(errorType)).send({ body, errorType });
res.status(getPluginErrorStatus(errorType)).send(errorType ? { body, errorType } : error);
}
};
};
180 changes: 130 additions & 50 deletions tests/gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ describe('Gateway', () => {
expect(gateway['pluginIndexUrl']).toBe('https://test-market-index-url.com');
});

it('run with ajv validator', async () => {
const mockResult = JSON.stringify({ success: true });
vi.mocked(SwaggerClient).mockResolvedValue({
execute: vi.fn().mockResolvedValue({
status: 200,
text: mockResult,
}),
});

describe('using AJV Validator', () => {
const gateway = new Gateway({
Validator: (schema, value) => {
const ajv = new Ajv({ validateFormats: false });
const ajv = new Ajv({ strict: false });
const validate = ajv.compile(schema);

const valid = validate(value);
Expand All @@ -38,53 +30,141 @@ describe('Gateway', () => {
},
});

const payload = {
apiName: 'getSupportedVendors',
arguments: '{}',
identifier: 'mock-credit-card',
manifest: {
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
api: [
{
description: 'It is API to get list of supported vendors',
name: 'getSupportedVendors',
parameters: {
properties: {},
type: 'object',
it('run with ajv validator', async () => {
const mockResult = JSON.stringify({ success: true });
vi.mocked(SwaggerClient).mockResolvedValue({
execute: vi.fn().mockResolvedValue({
status: 200,
text: mockResult,
}),
});

const payload = {
apiName: 'getSupportedVendors',
arguments: '{}',
identifier: 'mock-credit-card',
manifest: {
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
api: [
{
description: 'It is API to get list of supported vendors',
name: 'getSupportedVendors',
parameters: {
properties: {},
type: 'object',
},
},
],
author: 'arvinxx',
createdAt: '2023-12-11',
identifier: 'mock-credit-card',
meta: {
avatar: '馃挸',
description: 'Credit Card Number Generator',
tags: ['credit-card', 'mockup', 'generator'],
title: 'Credit Card Generator',
},
],
author: 'arvinxx',
createdAt: '2023-12-11',
identifier: 'mock-credit-card',
meta: {
avatar: '馃挸',
description: 'Credit Card Number Generator',
tags: ['credit-card', 'mockup', 'generator'],
title: 'Credit Card Generator',
openapi:
'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json',
settings: {
properties: {
apiKeyAuth: {
description: 'apiKeyAuth API Key',
format: 'password',
title: 'X-OpenAPIHub-Key',
type: 'string',
},
},
required: ['apiKeyAuth'],
type: 'object',
},
version: '1',
},
openapi:
'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json',
settings: {
properties: {
apiKeyAuth: {
description: 'apiKeyAuth API Key',
format: 'password',
title: 'X-OpenAPIHub-Key',
type: 'string',
type: 'default',
} as PluginRequestPayload;

const res = await gateway.execute(payload, { apiKeyAuth: 'abc' });

expect(res.success).toBeTruthy();
expect(res.data).toEqual(mockResult);
});

it('run with ajv with $$refs', async () => {
const mockResult = JSON.stringify({ success: true });
vi.mocked(SwaggerClient).mockResolvedValue({
execute: vi.fn().mockResolvedValue({
status: 200,
text: mockResult,
}),
});

const payload = {
apiName: 'getRandomCardNumber',
arguments: '{"vendor":"visa"}',
identifier: 'mock-credit-card',
manifest: {
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
api: [
{
description: 'It is API to generate random credit card numbers',
name: 'getRandomCardNumber',
parameters: {
properties: {
vendor: {
$$ref: 'https://chat-dev.lobehub.com/chat#/components/schemas/Vendor',
enum: [
'visa',
'master-card',
'diners-club',
'american-express',
'discover',
'jcb',
],
type: 'string',
},
},
required: ['vendor'],
type: 'object',
},
},
{
description: 'It is API to get list of supported vendors',
name: 'getSupportedVendors',
parameters: { properties: {}, type: 'object' },
},
],
author: 'arvinxx',
createdAt: '2023-12-11',
identifier: 'mock-credit-card',
meta: {
avatar: '馃挸',
description: 'Credit Card Number Generator',
tags: ['credit-card', 'mockup', 'generator'],
title: 'Credit Card Generator',
},
required: ['apiKeyAuth'],
type: 'object',
openapi:
'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json',
settings: {
properties: {
apiKeyAuth: {
description: 'apiKeyAuth API Key',
format: 'password',
title: 'X-OpenAPIHub-Key',
type: 'string',
},
},
required: ['apiKeyAuth'],
type: 'object',
},
version: '1',
},
version: '1',
},
type: 'default',
} as PluginRequestPayload;
type: 'default',
} as PluginRequestPayload;

const res = await gateway.execute(payload, { apiKeyAuth: 'abc' });
const res = await gateway.execute(payload, { apiKeyAuth: 'abc' });

expect(res.success).toBeTruthy();
expect(res.data).toEqual(mockResult);
expect(res.success).toBeTruthy();
expect(res.data).toEqual(mockResult);
});
});
});

0 comments on commit a6eb5b6

Please sign in to comment.