-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: install swagger, vision, inert for documentation and joi for va…
…lidation
- Loading branch information
Showing
13 changed files
with
345 additions
and
14 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
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,19 @@ | ||
import * as Inert from '@hapi/inert'; | ||
import * as Vision from '@hapi/vision'; | ||
import * as HapiSwagger from 'hapi-swagger'; | ||
import swaggerOptions from '../plugins/swagger'; | ||
|
||
const plugins: Array<any> = [ | ||
{ | ||
plugin: Inert, | ||
}, | ||
{ | ||
plugin: Vision, | ||
}, | ||
{ | ||
plugin: HapiSwagger, | ||
options: swaggerOptions, | ||
}, | ||
]; | ||
|
||
export default plugins; |
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,26 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { convertFromDirectory } from 'joi-to-typescript'; | ||
import { ApiError } from '../classes/ApiError'; | ||
import logger from './logger'; | ||
|
||
async function types(): Promise<void> { | ||
logger.info('🌴 Running joi-to-typescript...'); | ||
|
||
try { | ||
const result = await convertFromDirectory({ | ||
schemaDirectory: './src/schemas', | ||
typeOutputDirectory: './src/interfaces', | ||
debug: process.env.NODE_ENV === 'development', | ||
}); | ||
logger.info(result ? '🥂 Completed joi-to-typescript' : '😱 Failed to run joi-to-typescript'); | ||
} catch (err: any) { | ||
let currentError = err; | ||
if (!(currentError instanceof ApiError)) { | ||
currentError = new ApiError(err.status, err.message); | ||
currentError.originalErr = err; | ||
} | ||
throw currentError; | ||
} | ||
} | ||
|
||
types(); |
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,11 @@ | ||
/** | ||
* This file was automatically generated by joi-to-typescript | ||
* Do not modify this file manually | ||
*/ | ||
|
||
/** | ||
* a starter schema definition | ||
*/ | ||
export interface IStarter { | ||
message: string; | ||
} |
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,6 @@ | ||
/** | ||
* This file was automatically generated by joi-to-typescript | ||
* Do not modify this file manually | ||
*/ | ||
|
||
export * from './Starter'; |
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,12 @@ | ||
import * as HapiSwagger from 'hapi-swagger'; | ||
import * as Package from '../../package.json'; | ||
|
||
const swaggerOptions: HapiSwagger.RegisterOptions = { | ||
info: { | ||
title: 'Kitstarter API Documentation', | ||
description: 'Kitstarter API (v1) using Typescript with Hapi and Docker.', | ||
version: Package.version, | ||
}, | ||
}; | ||
|
||
export default swaggerOptions; |
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 |
---|---|---|
@@ -1,15 +1,35 @@ | ||
import { ServerRoute } from '@hapi/hapi'; | ||
import { sayHello } from '../controllers/starterCtrl'; | ||
import Joi from 'joi'; | ||
import { StarterSchema } from '../schemas/StarterSchema'; | ||
|
||
export const starterRoutes: ServerRoute[] = [ | ||
{ | ||
method: 'GET', | ||
path: '/hello', | ||
handler: sayHello, | ||
options: { | ||
description: 'Route test for kitstarter', | ||
notes: 'Say hello to a stranger', | ||
tags: ['api', 'starter'], | ||
handler: sayHello, | ||
response: { | ||
schema: Joi.array().items(StarterSchema), | ||
failAction: 'log', | ||
}, | ||
}, | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/hello/{name}', | ||
handler: sayHello, | ||
options: { | ||
description: 'Route test for kitstarter', | ||
notes: 'Say hello to someone', | ||
tags: ['api', 'starter'], | ||
handler: sayHello, | ||
response: { | ||
schema: Joi.array().items(StarterSchema), | ||
failAction: 'log', | ||
}, | ||
}, | ||
}, | ||
]; |
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,8 @@ | ||
import joi from 'joi'; | ||
|
||
export const StarterSchema = joi | ||
.object({ | ||
message: joi.string().required(), | ||
}) | ||
.meta({ className: 'IStarter' }) | ||
.description('a starter schema definition'); |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { Request } from '@hapi/hapi'; | ||
import { IStarter } from '../interfaces'; | ||
|
||
const getMessage = (params: Record<string, any>): string => | ||
`Hi ${params.name.charAt(0).toUpperCase() + params.name.slice(1)}`; | ||
const getMessage = (params: Record<string, any>): IStarter => { | ||
return { | ||
message: `Hi ${params.name.charAt(0).toUpperCase() + params.name.slice(1)}`, | ||
}; | ||
}; | ||
|
||
export { getMessage }; |
Oops, something went wrong.