diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-08-03 09:39:52 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-08-03 11:19:50 +0800 |
commit | 260640feed5dcda52936873397cf3b2c899718c6 (patch) | |
tree | ac843a20251ece74878152cc742dbe9806345dfe /packages/sra-api | |
parent | 36e7cb16aa086892ace18b2c6f3ed5a6e3b26a70 (diff) | |
download | dexon-0x-contracts-260640feed5dcda52936873397cf3b2c899718c6.tar.gz dexon-0x-contracts-260640feed5dcda52936873397cf3b2c899718c6.tar.zst dexon-0x-contracts-260640feed5dcda52936873397cf3b2c899718c6.zip |
Refactor using some utility methods
Diffstat (limited to 'packages/sra-api')
-rw-r--r-- | packages/sra-api/src/api.ts | 135 | ||||
-rw-r--r-- | packages/sra-api/src/errors.ts | 24 | ||||
-rw-r--r-- | packages/sra-api/src/examples/errors.ts | 11 | ||||
-rw-r--r-- | packages/sra-api/src/headers.ts | 20 | ||||
-rw-r--r-- | packages/sra-api/src/parameters.ts | 37 | ||||
-rw-r--r-- | packages/sra-api/src/responses.ts | 17 |
6 files changed, 135 insertions, 109 deletions
diff --git a/packages/sra-api/src/api.ts b/packages/sra-api/src/api.ts index 2a43e3e8e..33d4d3619 100644 --- a/packages/sra-api/src/api.ts +++ b/packages/sra-api/src/api.ts @@ -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', + 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', + }, }, - }, - ], - responses: { - '200': { - headers, - description: 'OK', - content: { - 'application/json': { - schema: { $ref: '#/components/schemas/relayerApiAssetDataPairsResponseSchema' }, - example: examples.relayerApiAssetDataPairsResponseSchema, + { + name: 'asset_data_b', + in: 'query', + description: 'The assetData value for the second asset in the pair.', + example: '0x0257179264389b814a946f3e92105513705ca6b990', + schema: { + $ref: '#/components/schemas/hexSchema', }, }, - }, - ...errorResponses, - }, + ], + true, + ), + responses: generateResponses( + 'relayerApiAssetDataPairsResponseSchema', + examples.relayerApiAssetDataPairsResponseSchema, + ), }, }, }, diff --git a/packages/sra-api/src/errors.ts b/packages/sra-api/src/errors.ts new file mode 100644 index 000000000..20c35514f --- /dev/null +++ b/packages/sra-api/src/errors.ts @@ -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.', + }, +}; diff --git a/packages/sra-api/src/examples/errors.ts b/packages/sra-api/src/examples/errors.ts new file mode 100644 index 000000000..81f29d81c --- /dev/null +++ b/packages/sra-api/src/examples/errors.ts @@ -0,0 +1,11 @@ +export const validationError = { + code: 100, + reason: 'Validation failed', + validationErrors: [ + { + field: 'networkId', + code: 1006, + reason: 'Network id 42 is not supported', + }, + ], +}; diff --git a/packages/sra-api/src/headers.ts b/packages/sra-api/src/headers.ts new file mode 100644 index 000000000..152254c9f --- /dev/null +++ b/packages/sra-api/src/headers.ts @@ -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', + }, + }, +}; diff --git a/packages/sra-api/src/parameters.ts b/packages/sra-api/src/parameters.ts new file mode 100644 index 000000000..dac898931 --- /dev/null +++ b/packages/sra-api/src/parameters.ts @@ -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]; +}; diff --git a/packages/sra-api/src/responses.ts b/packages/sra-api/src/responses.ts new file mode 100644 index 000000000..59c679b9c --- /dev/null +++ b/packages/sra-api/src/responses.ts @@ -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, +}); |