This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
generated from digicatapult/openapi-service-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from digicatapult/feature/clients
reorganising helper files/functions, helper routes file for clients, …
- Loading branch information
Showing
21 changed files
with
1,023 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const { | ||
POST_CLIENT_RESPONSES, | ||
validatePostClientResponse, | ||
} = require('../../validators/client/postClientResponseValidator') | ||
const { | ||
GET_CLIENTS_RESPONSES, | ||
validateGetClientsResponse, | ||
} = require('../../validators/client/getClientsResponseValidator') | ||
|
||
module.exports = function (apiService) { | ||
const doc = { | ||
GET: async function (req, res) { | ||
const { statusCode, result } = await apiService.getClients() | ||
|
||
const validationErrors = validateGetClientsResponse(statusCode, result) | ||
|
||
if (validationErrors) { | ||
res.status(statusCode).json(validationErrors) | ||
return | ||
} else { | ||
res.status(statusCode).json(result) | ||
return | ||
} | ||
}, | ||
POST: async function (req, res) { | ||
const { statusCode, result } = await apiService.postClient(req.body) | ||
|
||
const validationErrors = validatePostClientResponse(statusCode, result) | ||
|
||
if (validationErrors) { | ||
res.status(statusCode).json(validationErrors) | ||
return | ||
} else { | ||
res.status(statusCode).json(result) | ||
return | ||
} | ||
}, | ||
} | ||
|
||
doc.GET.apiDoc = { | ||
summary: 'Retrieve clients', | ||
responses: GET_CLIENTS_RESPONSES, | ||
tags: ['clients'], | ||
} | ||
|
||
doc.POST.apiDoc = { | ||
summary: 'Create client', | ||
requestBody: { | ||
content: { | ||
'application/json': { | ||
schema: { | ||
$ref: '#/components/schemas/PostAndPutClient', | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: POST_CLIENT_RESPONSES, | ||
tags: ['clients'], | ||
} | ||
|
||
return doc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
const { | ||
validateGetClientByIdResponse, | ||
GET_CLIENT_BY_ID_RESPONSES, | ||
} = require('../../../validators/client/getClientByIdResponseValidator') | ||
const { | ||
PUT_CLIENT_RESPONSES, | ||
validatePutClientResponse, | ||
} = require('../../../validators/client/putClientResponseValidator') | ||
const { | ||
validateDeleteClientResponse, | ||
DELETE_CLIENT_RESPONSES, | ||
} = require('../../../validators/client/deleteClientResponseValidator') | ||
|
||
module.exports = function (apiService) { | ||
const doc = { | ||
GET: async function (req, res) { | ||
const { id } = req.params | ||
const { statusCode, result } = await apiService.getClientById(id) | ||
|
||
const validationErrors = validateGetClientByIdResponse(statusCode, result) | ||
|
||
if (validationErrors) { | ||
res.status(statusCode).json(validationErrors) | ||
return | ||
} else { | ||
res.status(statusCode).json(result) | ||
return | ||
} | ||
}, | ||
PUT: async function (req, res) { | ||
const { id } = req.params | ||
const { statusCode, result } = await apiService.putClient(id, req.body) | ||
|
||
const validationErrors = validatePutClientResponse(statusCode, result) | ||
|
||
if (validationErrors) { | ||
res.status(statusCode).json(validationErrors) | ||
return | ||
} else { | ||
res.status(statusCode).json(result) | ||
return | ||
} | ||
}, | ||
DELETE: async function (req, res) { | ||
const { id } = req.params | ||
const { statusCode, result } = await apiService.deleteClient(id) | ||
|
||
const validationErrors = validateDeleteClientResponse(statusCode, result) | ||
|
||
if (validationErrors) { | ||
res.status(statusCode).json(validationErrors) | ||
return | ||
} else { | ||
res.status(statusCode).send() | ||
} | ||
}, | ||
} | ||
|
||
doc.GET.apiDoc = { | ||
summary: 'Retrieve client by id', | ||
parameters: [ | ||
{ | ||
description: 'Id of the client', | ||
in: 'path', | ||
required: true, | ||
name: 'id', | ||
schema: { | ||
type: 'string', | ||
format: 'uuid', | ||
}, | ||
}, | ||
], | ||
responses: GET_CLIENT_BY_ID_RESPONSES, | ||
tags: ['clients'], | ||
} | ||
|
||
doc.PUT.apiDoc = { | ||
summary: 'Update client', | ||
parameters: [ | ||
{ | ||
description: 'Id of the client', | ||
in: 'path', | ||
required: true, | ||
name: 'id', | ||
schema: { | ||
type: 'string', | ||
format: 'uuid', | ||
}, | ||
}, | ||
], | ||
requestBody: { | ||
content: { | ||
'application/json': { | ||
schema: { | ||
$ref: '#/components/schemas/PostAndPutClient', | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: PUT_CLIENT_RESPONSES, | ||
tags: ['clients'], | ||
} | ||
|
||
doc.DELETE.apiDoc = { | ||
summary: 'Remove client', | ||
parameters: [ | ||
{ | ||
description: 'Id of the client', | ||
in: 'path', | ||
required: true, | ||
name: 'id', | ||
schema: { | ||
type: 'string', | ||
format: 'uuid', | ||
}, | ||
}, | ||
], | ||
responses: DELETE_CLIENT_RESPONSES, | ||
tags: ['clients'], | ||
} | ||
|
||
return doc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/api-v1/validators/client/deleteClientResponseValidator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { default: OpenAPIResponseValidator } = require('openapi-response-validator') | ||
|
||
const apiDocResponses = require('../../api-doc-responses') | ||
|
||
const DELETE_CLIENT_RESPONSES = { | ||
204: { | ||
description: 'Deleted client', | ||
}, | ||
404: apiDocResponses['404'], | ||
default: apiDocResponses.default, | ||
} | ||
|
||
const validateDeleteClientResponse = (statusCode, result) => { | ||
const responseValidator = new OpenAPIResponseValidator({ responses: DELETE_CLIENT_RESPONSES }) | ||
|
||
return responseValidator.validateResponse(statusCode, result) | ||
} | ||
|
||
module.exports = { | ||
DELETE_CLIENT_RESPONSES, | ||
validateDeleteClientResponse, | ||
} |
28 changes: 28 additions & 0 deletions
28
app/api-v1/validators/client/getClientByIdResponseValidator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { default: OpenAPIResponseValidator } = require('openapi-response-validator') | ||
|
||
const apiDocResponses = require('../../api-doc-responses') | ||
const apiDoc = require('../../api-doc') | ||
|
||
const GET_CLIENT_BY_ID_RESPONSES = { | ||
200: { | ||
description: 'Retrieved client by id', | ||
content: { | ||
'application/json': { | ||
schema: apiDoc.components.schemas.GetClient, | ||
}, | ||
}, | ||
}, | ||
404: apiDocResponses['404'], | ||
default: apiDocResponses.default, | ||
} | ||
|
||
const validateGetClientByIdResponse = (statusCode, result) => { | ||
const responseValidator = new OpenAPIResponseValidator({ responses: GET_CLIENT_BY_ID_RESPONSES }) | ||
|
||
return responseValidator.validateResponse(statusCode, result) | ||
} | ||
|
||
module.exports = { | ||
GET_CLIENT_BY_ID_RESPONSES, | ||
validateGetClientByIdResponse, | ||
} |
30 changes: 30 additions & 0 deletions
30
app/api-v1/validators/client/getClientsResponseValidator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { default: OpenAPIResponseValidator } = require('openapi-response-validator') | ||
|
||
const apiDocResponses = require('../../api-doc-responses') | ||
const apiDoc = require('../../api-doc') | ||
|
||
const GET_CLIENTS_RESPONSES = { | ||
200: { | ||
description: 'Retrieved clients', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: apiDoc.components.schemas.GetClient, | ||
}, | ||
}, | ||
}, | ||
}, | ||
default: apiDocResponses.default, | ||
} | ||
|
||
const validateGetClientsResponse = (statusCode, result) => { | ||
const responseValidator = new OpenAPIResponseValidator({ responses: GET_CLIENTS_RESPONSES }) | ||
|
||
return responseValidator.validateResponse(statusCode, result) | ||
} | ||
|
||
module.exports = { | ||
GET_CLIENTS_RESPONSES, | ||
validateGetClientsResponse, | ||
} |
Oops, something went wrong.