Add generic to APIResponse
This commit is contained in:
parent
248b24e612
commit
c3f2779650
@ -1,10 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* Base type for an API response format.
|
* Base type for an API response format.
|
||||||
*/
|
*/
|
||||||
export interface APIResponse {
|
export interface APIResponse<T> {
|
||||||
success: boolean,
|
success: boolean,
|
||||||
message?: string,
|
message?: string,
|
||||||
data?: any,
|
data?: T,
|
||||||
error?: {
|
error?: {
|
||||||
name: string,
|
name: string,
|
||||||
message: string,
|
message: string,
|
||||||
@ -17,7 +17,7 @@ export interface APIResponse {
|
|||||||
* @param {string} displayMessage
|
* @param {string} displayMessage
|
||||||
* @return APIResponse
|
* @return APIResponse
|
||||||
*/
|
*/
|
||||||
export function message(displayMessage: string): APIResponse {
|
export function message(displayMessage: string): APIResponse<void> {
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: displayMessage,
|
message: displayMessage,
|
||||||
@ -29,7 +29,7 @@ export function message(displayMessage: string): APIResponse {
|
|||||||
* @param record
|
* @param record
|
||||||
* @return APIResponse
|
* @return APIResponse
|
||||||
*/
|
*/
|
||||||
export function one(record: unknown): APIResponse {
|
export function one<T>(record: T): APIResponse<T> {
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: record,
|
data: record,
|
||||||
@ -41,7 +41,7 @@ export function one(record: unknown): APIResponse {
|
|||||||
* @param {array} records
|
* @param {array} records
|
||||||
* @return APIResponse
|
* @return APIResponse
|
||||||
*/
|
*/
|
||||||
export function many(records: any[]): APIResponse {
|
export function many<T>(records: T[]): APIResponse<{records: T[], total: number}> {
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
@ -56,7 +56,7 @@ export function many(records: any[]): APIResponse {
|
|||||||
* @return APIResponse
|
* @return APIResponse
|
||||||
* @param thrownError
|
* @param thrownError
|
||||||
*/
|
*/
|
||||||
export function error(thrownError: string | Error): APIResponse {
|
export function error(thrownError: string | Error): APIResponse<void> {
|
||||||
if ( typeof thrownError === 'string' ) {
|
if ( typeof thrownError === 'string' ) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
|
Loading…
Reference in New Issue
Block a user