Skip to content

Instantly share code, notes, and snippets.

@chiro-hiro
Last active October 18, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiro-hiro/cf4fdf757a12efdf5b67a5bbe51271ef to your computer and use it in GitHub Desktop.
Save chiro-hiro/cf4fdf757a12efdf5b67a5bbe51271ef to your computer and use it in GitHub Desktop.
Standardized your RESTful API in TypeScript
export interface IError {
  message: string;
  code?: string;
  stack?: string;
}

export interface IOrdering {
  column: string;
  order: 'asc' | 'desc';
}

export interface IPagination {
  offset: number;
  limit: number;
  order: IOrdering[];
}

export interface IRecordList<T> extends IPagination {
  total: number;
  records: T[];
}

export interface IBasicResponse {
  success: boolean;
  deprecated?: boolean;
}

export interface IResponseRecord<T> extends IBasicResponse {
  result: T;
}

export interface IResponseList<T> extends IBasicResponse {
  result: IRecordList<T>;
}

export interface IResponseError extends IBasicResponse {
  result: IError;
}

export interface IResponseCommon<T> extends IBasicResponse {
  result: IError | IRecordList<T> | T;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment