Add generic to APIResponse

This commit is contained in:
Garrett Mills 2021-10-19 13:00:14 -05:00
parent 248b24e612
commit c3f2779650

View File

@ -1,10 +1,10 @@
/**
* Base type for an API response format.
*/
export interface APIResponse {
export interface APIResponse<T> {
success: boolean,
message?: string,
data?: any,
data?: T,
error?: {
name: string,
message: string,
@ -17,7 +17,7 @@ export interface APIResponse {
* @param {string} displayMessage
* @return APIResponse
*/
export function message(displayMessage: string): APIResponse {
export function message(displayMessage: string): APIResponse<void> {
return {
success: true,
message: displayMessage,
@ -29,7 +29,7 @@ export function message(displayMessage: string): APIResponse {
* @param record
* @return APIResponse
*/
export function one(record: unknown): APIResponse {
export function one<T>(record: T): APIResponse<T> {
return {
success: true,
data: record,
@ -41,7 +41,7 @@ export function one(record: unknown): APIResponse {
* @param {array} records
* @return APIResponse
*/
export function many(records: any[]): APIResponse {
export function many<T>(records: T[]): APIResponse<{records: T[], total: number}> {
return {
success: true,
data: {
@ -56,7 +56,7 @@ export function many(records: any[]): APIResponse {
* @return APIResponse
* @param thrownError
*/
export function error(thrownError: string | Error): APIResponse {
export function error(thrownError: string | Error): APIResponse<void> {
if ( typeof thrownError === 'string' ) {
return {
success: false,