@fababracx/ngx-mock-api
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

NgxMockApi

Mock api library for Angular

Usage

  • In AppModule add in import : HttpClientModule and NgxMockApiModule.withConfig(activeMockApi, mockApiUrls, delayRequest)

    • with params: const activeMockApi: true;
    • and params: const delayRequest: 0;
    • and
const mockApiUrlsExemple: INgxMockApiUrlsConfig[] = [
  {
    method: HttpMethod.GET, // Default value HttpMethod.GET
    url: 'http:localhost:4200/users',
    data: [
      {
        id: 1,
        name: 'User 1'
      },
      {
        id: 2,
        name: 'User 2'
      }
    ],
    compareType: CompareType.EQUALS, // Default value CompareType.EQUALS
    status: 200 // Default value 200,
    errorMessage: undefined, // Displayed message if error
    //WIP: callback: undefined // callback after first request
  },
  ...NgxMockApiUrlsConfig.generateNgxMockApiUrlConfig('http://localhost:4200', ['generate'], ['generate']), // generateNgxMockApiUrlConfig(apiUrl?: string, endpoints?: string[], datas?: any[], methods?: HttpMethod[], compareType?: CompareType, statuses?: number[], errorMessages?: string[])
];
  • Allowed compare types

    • CompareType.EQUALS or 'Equals'

      • This compare type check if mock Url match regex

        /((https?:\/\/).*(\/.*)?)(\:.*)?$/gm;
    • CompareType.STARTS_WITH or 'StartsWith'

    • CompareType.ENDS_WITH or 'EndsWith'

  • Allowed http method

    • HttpMethod.GET or 'GET'
    • HttpMethod.POST or 'POST'
    • HttpMethod.PUT or 'PUT'
    • HttpMethod.PATCH or 'PATCH'
    • HttpMethod.DELETE or 'DELETE'
    • HttpMethod.OPTIONS or 'OPTIONS'
  • Just call your api with your service

  get users$(): Observable<any> {
    return this.http.get('http://localhost:4200/users');
  }

  get generate$(): Observable<any> {
    return this.http.get('http://localhost:4200/generate');
  }

Package Sidebar

Install

npm i @fababracx/ngx-mock-api

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

82.7 kB

Total Files

26

Last publish

Collaborators

  • fababracx