loopback-content-range icon indicating copy to clipboard operation
loopback-content-range copied to clipboard

loopback 4 compatibility

Open mcelotti opened this issue 6 years ago • 2 comments

Any plans to release this for lp4? Thanks

mcelotti avatar Dec 17 '18 10:12 mcelotti

Hi @mcelotti No plan yet.

darthwesker avatar Feb 12 '19 15:02 darthwesker

Hello, for those interested in loopback 4 equivalent, you can:

  1. create an interceptor (not a global one) with command "lb4 interceptor" (I called mine Contentrange)
  2. Modify it like this:

` async intercept(invocationCtx: InvocationContext, next: () => ValueOrPromise<InvocationResult>) { const result = await next(); const res = await invocationCtx.get(RestBindings.Http.RESPONSE, {optional: true});

  if (res && !res.headersSent) {
    const args = invocationCtx.args.length ? invocationCtx.args[0] : {};
    let offset = 0;
    let filter;
    let limit = 0;

    if (args.where)
      filter = args.where;

    if (
      args.limit == null ||
      args.limit !== parseInt(args.limit, 10)
    )
      args.limit = limit;
    else
      limit = args.limit;

    if (args.offset)
      offset = args.offset;
    else
      args.offset = offset;

    if (typeof (invocationCtx.target as AnyObject).count === 'function') {
      const {count} = await (invocationCtx.target as AnyObject).count(filter);
      limit = limit === 0 ? count : limit;
      const last = Math.min(offset + limit, count);
      res.set('Access-Control-Expose-Headers', 'Content-Range');
      res.set(
          'Content-Range',
          `${offset}-${last}/${count}`
        );
    }
  }
  return result;
}

}`

  1. In your controllers, add the decorator: @intercept(ContentrangeInterceptor.BINDING_KEY)

bv86 avatar Mar 01 '21 16:03 bv86