loopback-content-range
loopback-content-range copied to clipboard
loopback 4 compatibility
Any plans to release this for lp4? Thanks
Hi @mcelotti No plan yet.
Hello, for those interested in loopback 4 equivalent, you can:
- create an interceptor (not a global one) with command "lb4 interceptor" (I called mine Contentrange)
- 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;
}
}`
- In your controllers, add the decorator:
@intercept(ContentrangeInterceptor.BINDING_KEY)