Refactor using some utility methods

This commit is contained in:
fragosti
2018-08-02 18:39:52 -07:00
parent 36e7cb16aa
commit 260640feed
6 changed files with 136 additions and 110 deletions

View File

@@ -2,86 +2,11 @@ import { schemas } from '@0xproject/json-schemas';
import { OpenApiSpec } from '@loopback/openapi-v3-types';
import { examples } from './examples';
import { generateParameters } from './parameters';
import { generateResponses } from './responses';
// We need to replace the `$ref`s to be openAPI compliant.
const openApiSchemas = JSON.parse(JSON.stringify(schemas).replace(/(\/\w+)/g, match => `#/components/schemas${match}`));
const paginationParameters = [
{
name: 'page',
in: 'query',
description: 'The number of the page to request in the collection.',
example: 3,
schema: {
type: 'number',
},
},
{
name: 'per_page',
in: 'query',
description: 'The number of records to return per page.',
example: 10,
schema: {
type: 'number',
},
},
];
const networkdIdParameter = {
name: 'network_id',
in: 'query',
description: 'The id of the Ethereum network',
example: 42,
default: 1,
schema: {
type: 'number',
},
};
const headers = {
'X-Rate-Limit-Limit': {
description: `The maximum number of requests you're permitted to make per hour.`,
schema: {
type: 'integer',
},
},
'X-Rate-Limit-Remaining': {
description: 'The number of requests remaining in the current rate limit window.',
schema: {
type: 'integer',
},
},
'X-Rate-Limit-Reset': {
description: 'The time at which the current rate limit window resets in UTC epoch seconds.',
schema: {
type: 'integer',
},
},
};
const errorResponses = {
'400': {
description: 'Validation error',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/relayerApiErrorResponseSchema' },
example: examples.validationError,
},
},
},
'404': {
description: 'Not found',
},
'429': {
description: 'Too many requests - Rate limit exceeded',
},
'500': {
description: 'Internal Server Error',
},
'501': {
description: 'Not implemented.',
},
};
export const api: OpenApiSpec = {
openapi: '3.0.0',
info: {
@@ -103,41 +28,33 @@ export const api: OpenApiSpec = {
description:
'Retrieves a list of available asset pairs and the information required to trade them (in any order). Setting only `asset_data_a` or `asset_data_b` returns pairs filtered by that asset only.',
operationId: 'getAssetPairs',
parameters: [
networkdIdParameter,
...paginationParameters,
{
name: 'asset_data_a',
in: 'query',
description: 'The assetData value for the first asset in the pair.',
example: '0xf47261b04c32345ced77393b3530b1eed0f346429d',
schema: {
$ref: '#/components/schemas/hexSchema',
},
},
{
name: 'asset_data_b',
in: 'query',
description: 'The assetData value for the second asset in the pair.',
example: '0x0257179264389b814a946f3e92105513705ca6b990',
schema: {
$ref: '#/components/schemas/hexSchema',
},
},
],
responses: {
'200': {
headers,
description: 'OK',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/relayerApiAssetDataPairsResponseSchema' },
example: examples.relayerApiAssetDataPairsResponseSchema,
parameters: generateParameters(
[
{
name: 'asset_data_a',
in: 'query',
description: 'The assetData value for the first asset in the pair.',
example: '0xf47261b04c32345ced77393b3530b1eed0f346429d',
schema: {
$ref: '#/components/schemas/hexSchema',
},
},
},
...errorResponses,
},
{
name: 'asset_data_b',
in: 'query',
description: 'The assetData value for the second asset in the pair.',
example: '0x0257179264389b814a946f3e92105513705ca6b990',
schema: {
$ref: '#/components/schemas/hexSchema',
},
},
],
true,
),
responses: generateResponses(
'relayerApiAssetDataPairsResponseSchema',
examples.relayerApiAssetDataPairsResponseSchema,
),
},
},
},

View File

@@ -0,0 +1,24 @@
import { examples } from './examples';
export const errorResponses = {
'400': {
description: 'Validation error',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/relayerApiErrorResponseSchema' },
example: examples.validationError,
},
},
},
'404': {
description: 'Not found',
},
'429': {
description: 'Too many requests - Rate limit exceeded',
},
'500': {
description: 'Internal Server Error',
},
'501': {
description: 'Not implemented.',
},
};

View File

@@ -0,0 +1,11 @@
export const validationError = {
code: 100,
reason: 'Validation failed',
validationErrors: [
{
field: 'networkId',
code: 1006,
reason: 'Network id 42 is not supported',
},
],
};

View File

@@ -0,0 +1,20 @@
export const headers = {
'X-Rate-Limit-Limit': {
description: `The maximum number of requests you're permitted to make per hour.`,
schema: {
type: 'integer',
},
},
'X-Rate-Limit-Remaining': {
description: 'The number of requests remaining in the current rate limit window.',
schema: {
type: 'integer',
},
},
'X-Rate-Limit-Reset': {
description: 'The time at which the current rate limit window resets in UTC epoch seconds.',
schema: {
type: 'integer',
},
},
};

View File

@@ -0,0 +1,37 @@
import { ParameterLocation, ParameterObject } from '@loopback/openapi-v3-types';
export const paginationParameters: ParameterObject[] = [
{
name: 'page',
in: 'query',
description: 'The number of the page to request in the collection.',
example: 3,
schema: {
type: 'number',
},
},
{
name: 'per_page',
in: 'query',
description: 'The number of records to return per page.',
example: 10,
schema: {
type: 'number',
},
},
];
export const networkdIdParameter = {
name: 'network_id',
in: 'query',
description: 'The id of the Ethereum network',
example: 42,
default: 1,
schema: {
type: 'number',
},
};
export const generateParameters = (parameters: ParameterObject[], isPaginated: boolean = false): ParameterObject[] => {
const optionalParameters = isPaginated ? paginationParameters : [];
return [...optionalParameters, ...parameters];
};

View File

@@ -0,0 +1,17 @@
import { ResponsesObject } from '@loopback/openapi-v3-types';
import { errorResponses } from './errors';
import { headers } from './headers';
export const generateResponses = (schemaName: string, example: any): ResponsesObject => ({
'200': {
headers,
description: 'OK',
content: {
'application/json': {
schema: { $ref: `#/components/schemas/${schemaName}` },
},
},
},
...errorResponses,
});