Skip to content

Commit

Permalink
Merge pull request #19 from KaungWai/feature/12-shared-schema
Browse files Browse the repository at this point in the history
feat(schema): enhance schema autoload mechanism
  • Loading branch information
KaungWai authored Sep 15, 2023
2 parents c5118b9 + 7eacc5e commit e7bdbde
Show file tree
Hide file tree
Showing 27 changed files with 197 additions and 174 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ms-azuretools.vscode-docker",
"vscode-icons-team.vscode-icons",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
"esbenp.prettier-vscode",
"yoavbls.pretty-ts-errors"
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
},
Expand Down
132 changes: 44 additions & 88 deletions sdk/kickstart_fastify_1.0.4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ export interface LoginRequest {

export type LoginResult = object

export interface LoginResponse {
result: LoginResult
message?: string
}

export type LogoutResult = object

export interface LogoutResponse {
result: LogoutResult
message?: string
}

export interface CreateProductRequest {
/**
* @minLength 1
Expand Down Expand Up @@ -57,6 +67,11 @@ export interface CreateProductResult {
productPrice: number
}

export interface CreateProductResponse {
result: CreateProductResult
message?: string
}

export interface DeleteProductByIdParams {
/**
* @minLength 12
Expand All @@ -67,6 +82,11 @@ export interface DeleteProductByIdParams {

export type DeleteProductByIdResult = object

export interface DeleteProductByIdResponse {
result: DeleteProductByIdResult
message?: string
}

export interface GetProductByIdParams {
/**
* @minLength 12
Expand Down Expand Up @@ -95,6 +115,11 @@ export interface GetProductByIdResult {
productPrice: number
}

export interface GetProductByIdResponse {
result: GetProductByIdResult
message?: string
}

export interface GetProductsQuery {
/**
* @minLength 12
Expand Down Expand Up @@ -137,6 +162,11 @@ export type GetProductsResult = {
productPrice: number
}[]

export interface GetProductsResponse {
result: GetProductsResult
message?: string
}

export interface UpdateProductRequest {
/**
* @minLength 12
Expand Down Expand Up @@ -177,6 +207,11 @@ export interface UpdateProductResult {
productPrice: number
}

export interface UpdateProductResponse {
result: UpdateProductResult
message?: string
}

export type QueryParamsType = Record<string | number, any>
export type ResponseFormat = keyof Omit<Body, 'body' | 'bodyUsed'>

Expand Down Expand Up @@ -382,13 +417,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
* @request POST:/auth/login
*/
login: (data: LoginRequest, params: RequestParams = {}) =>
this.request<
{
result: LoginResult
message?: string
},
any
>({
this.request<LoginResponse, any>({
path: `/auth/login`,
method: 'POST',
body: data,
Expand All @@ -406,13 +435,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
* @request POST:/auth/logout
*/
logout: (params: RequestParams = {}) =>
this.request<
{
result: LogoutResult
message?: string
},
any
>({
this.request<LogoutResponse, any>({
path: `/auth/logout`,
method: 'POST',
format: 'json',
Expand All @@ -428,30 +451,8 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
* @summary create a new product
* @request POST:/product
*/
createProduct: (
data: {
/**
* @minLength 1
* @maxLength 100
*/
productName: string
/**
* @minLength 1
* @maxLength 500
*/
productDescription: string
/** @min 1 */
productPrice: number
},
params: RequestParams = {},
) =>
this.request<
{
result: CreateProductResult
message?: string
},
any
>({
createProduct: (data: CreateProductRequest, params: RequestParams = {}) =>
this.request<CreateProductResponse, any>({
path: `/product`,
method: 'POST',
body: data,
Expand Down Expand Up @@ -492,13 +493,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
},
params: RequestParams = {},
) =>
this.request<
{
result: GetProductsResult
message?: string
},
any
>({
this.request<GetProductsResponse, any>({
path: `/product`,
method: 'GET',
query: query,
Expand All @@ -514,35 +509,8 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
* @summary update a product
* @request PUT:/product
*/
updateProduct: (
data: {
/**
* @minLength 12
* @maxLength 12
*/
productId: string
/**
* @minLength 1
* @maxLength 100
*/
productName: string
/**
* @minLength 1
* @maxLength 500
*/
productDescription: string
/** @min 1 */
productPrice: number
},
params: RequestParams = {},
) =>
this.request<
{
result: UpdateProductResult
message?: string
},
any
>({
updateProduct: (data: UpdateProductRequest, params: RequestParams = {}) =>
this.request<UpdateProductResponse, any>({
path: `/product`,
method: 'PUT',
body: data,
Expand All @@ -560,13 +528,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
* @request GET:/product/{productId}
*/
getProductById: (productId: string, params: RequestParams = {}) =>
this.request<
{
result: GetProductByIdResult
message?: string
},
any
>({
this.request<GetProductByIdResponse, any>({
path: `/product/${productId}`,
method: 'GET',
format: 'json',
Expand All @@ -582,13 +544,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
* @request DELETE:/product/{productId}
*/
deleteProduct: (productId: string, params: RequestParams = {}) =>
this.request<
{
result: DeleteProductByIdResult
message?: string
},
any
>({
this.request<DeleteProductByIdResponse, any>({
path: `/product/${productId}`,
method: 'DELETE',
format: 'json',
Expand Down
2 changes: 2 additions & 0 deletions src/constants/systemConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const SYS_CONSTANTS: {
JWT_COOKIE_KEY: string
SALT_ROUNDS: number
SWAGGER_ROUTE: string
SCHEMA_SYMBOL: symbol
} = {
NANOID_LENGTH: 12,
DEFAULT_ENCODING: 'utf-8',
JWT_COOKIE_KEY: 'jwt',
SALT_ROUNDS: 10,
SWAGGER_ROUTE: 'docs',
SCHEMA_SYMBOL: Symbol('Schema symbol'),
}
15 changes: 9 additions & 6 deletions src/handlers/auth/login/request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Static, Type } from '@sinclair/typebox'

import { field } from '@/schemas/fields'
import { handlerUtils } from '@/utils/handler'

export const loginRequest = Type.Object(
{
userId: field.userId,
password: Type.String(),
},
{ $id: 'loginRequest' },
export const loginRequest = handlerUtils.buildRequestSchema(
Type.Object(
{
userId: field.userId,
password: Type.String(),
},
{ $id: 'loginRequest' },
),
)

export type LoginRequest = Static<typeof loginRequest>
4 changes: 2 additions & 2 deletions src/handlers/auth/login/response.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Static, Type } from '@sinclair/typebox'

import { createDefaultResponseSchema } from '@/handlers/base/defaultResponse'
import { handlerUtils } from '@/utils/handler'

export const loginResult = Type.Object({}, { $id: 'loginResult' })

export const loginResponse = createDefaultResponseSchema(loginResult, 'loginResponse')
export const loginResponse = handlerUtils.buildResponseSchema(loginResult, 'loginResponse')

export type LoginResult = Static<typeof loginResult>

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/auth/login/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export const loginSchema: FastifySchema = {
description: description,
body: Type.Ref(loginRequest),
response: {
200: loginResponse,
200: Type.Ref(loginResponse),
},
}
4 changes: 2 additions & 2 deletions src/handlers/auth/logout/response.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Static, Type } from '@sinclair/typebox'

import { createDefaultResponseSchema } from '@/handlers/base/defaultResponse'
import { handlerUtils } from '@/utils/handler'

export const logoutResult = Type.Object({}, { $id: 'logoutResult' })

export const logoutResponse = createDefaultResponseSchema(logoutResult, 'logoutResponse')
export const logoutResponse = handlerUtils.buildResponseSchema(logoutResult, 'logoutResponse')

export type LogoutResult = Static<typeof logoutResult>

Expand Down
3 changes: 2 additions & 1 deletion src/handlers/auth/logout/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Type } from '@sinclair/typebox'
import { FastifySchema } from 'fastify'

import { logoutResponse } from './response'
Expand All @@ -14,6 +15,6 @@ export const logoutSchema: FastifySchema = {
tags: ['Auth'],
description: description,
response: {
200: logoutResponse,
200: Type.Ref(logoutResponse),
},
}
18 changes: 0 additions & 18 deletions src/handlers/base/defaultResponse.ts

This file was deleted.

17 changes: 10 additions & 7 deletions src/handlers/product/createProduct/request.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Static, Type } from '@sinclair/typebox'

import { field } from '@/schemas/fields'
import { handlerUtils } from '@/utils/handler'

export const createProductRequest = Type.Object(
{
productName: field.productName,
productDescription: field.productDescription,
productPrice: field.productPrice,
},
{ $id: 'createProductRequest' },
export const createProductRequest = handlerUtils.buildRequestSchema(
Type.Object(
{
productName: field.productName,
productDescription: field.productDescription,
productPrice: field.productPrice,
},
{ $id: 'createProductRequest' },
),
)

export type CreateProductRequest = Static<typeof createProductRequest>
5 changes: 3 additions & 2 deletions src/handlers/product/createProduct/response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Static, Type } from '@sinclair/typebox'

import { createDefaultResponseSchema } from '@/handlers/base/defaultResponse'
import { field } from '@/schemas/fields'

import { handlerUtils } from './../../../utils/handler'

export const createProductResult = Type.Object(
{
productId: field.productId,
Expand All @@ -13,7 +14,7 @@ export const createProductResult = Type.Object(
{ $id: 'createProductResult' },
)

export const createProductResponse = createDefaultResponseSchema(createProductResult, 'createProductResponse')
export const createProductResponse = handlerUtils.buildResponseSchema(createProductResult, 'createProductResponse')

export type CreateProductResult = Static<typeof createProductResult>

Expand Down
Loading

0 comments on commit e7bdbde

Please sign in to comment.