-
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.
Update: unified api-response structure
- Loading branch information
1 parent
731a88d
commit 5faf7c1
Showing
5 changed files
with
39 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { HttpException } from '../http-exception/index.service'; | ||
|
||
export interface SuccessType<DataType> { | ||
success: boolean; | ||
data: DataType; | ||
} | ||
|
||
export interface ErrorType extends HttpException {} | ||
|
||
export class ApiResponse { | ||
public error(statusCode: number, message: string) { | ||
return new HttpException(statusCode, message); | ||
} | ||
|
||
public success<D = Record<string, unknown>>(data: D) { | ||
return { success: true, data }; | ||
} | ||
} |
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,20 @@ | ||
import { ApiResponse } from './index.service'; | ||
|
||
describe('response', () => { | ||
const apiResponse = new ApiResponse(); | ||
|
||
it('should return success response', () => { | ||
expect(apiResponse.success({ name: 'Foo Bar Baz' })).toMatchObject({ | ||
success: true, | ||
data: { name: 'Foo Bar Baz' }, | ||
}); | ||
}); | ||
|
||
it('should return error response', () => { | ||
expect(apiResponse.error(400, 'Invalid email')).toMatchObject({ | ||
success: false, | ||
statusCode: 400, | ||
message: 'Invalid email', | ||
}); | ||
}); | ||
}); |
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,2 +1,2 @@ | ||
export { HttpException } from './http-exception/index.service'; | ||
export { response, ResponseType } from './response/index.service'; | ||
export { ApiResponse, ErrorType, SuccessType } from './api-response/index.service'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.