This project is archived. Its data is read-only.

Use fake backend - backendless development

What needs to be changed?

We can use backendless development so that we are not dependent on having running backend to provide data for the app.

How is the current state not sufficient?

We don't have fully functional up-to-date backend.

Which changes are necessary?

BackendInterceptor which implements HttpInterceptor needs to be coded (as described in the link above) with its intercept method which covers all links that we need dummy backend response for.

For example this simple demo example would provide a sample thing whenever a /search URL is requested (via a POST request):


const sampleThing: SCMessage = {
  audiences: ['students'],
  message: 'Foo Message',
  name: 'foo',
  origin: {
    indexed: 'SOME-DATE',
    name: 'some name',
  },
  type: 'message',
  uid: '123',
};


@Injectable()
export class FakeBackendInterceptor implements HttpInterceptor {

  constructor() {
    // @TODO if needed
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (request.url.endsWith('/search') && request.method === 'POST') {
      // respond 200 OK
      return of(new HttpResponse({status: 200, body: {data: [sampleThing]}}));
    } else {
      // For all other requests - forward the requests to the demanded URLs (backend)
      return next.handle(request);
    }
  }
}

This FakeBackendInterceptor needs to be then added to e.g. app.module.ts, to be available throughout the app (or directly into test modules):

  providers: [
{
  // use fake backend in place of Http service for backend-less development
  multi: true,
  provide: HTTP_INTERCEPTORS,
  useClass: FakeBackendInterceptor,
}

Do the proposed changes impact current use cases?

It impacts in the way that dummy predefined data will be provided to the app, and not the real data from the backend. So after not needed anymore, these interceptions need to be deactivated, so that real backends are used.

cc: @sebastianlange

Edited Feb 20, 2019 by Jovan Krunić
Assignee Loading
Time tracking Loading