8000 feat(http): Add `redirect` property to hook context by vonagam · Pull Request #2484 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/express/src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ export const serviceMiddleware = (callback: ServiceCallback) =>
const { query, body: data } = req;
const { __feathersId: id = null, ...route } = req.params;
const params = { query, route, ...req.feathers };

const context = await callback(req, res, { id, data, params });

const result = http.getData(context);
const statusCode = http.getStatusCode(context, result);
const location = http.getLocation(context, req.get('Referrer'));

res.data = result;
res.status(http.getStatusCode(context, result));
res.status(statusCode);
if (location) res.set('Location', location);

next();
} catch (error: any) {
Expand All @@ -66,11 +71,13 @@ export const serviceMethodHandler = (
}

const args = getArgs(options);
const context = createContext(service, method);
const contextBase = createContext(service, method);
res.hook = contextBase;

const context = await service[method](...args, contextBase);
res.hook = context;

return service[method](...args, context);
return context;
});

export function rest (handler: RequestHandler = formatter) {
Expand Down
4 changes: 4 additions & 0 deletions packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
* code that should be returned.
*/
statusCode?: number;
/**
* A writeable, optional property that allows to set a redirect location.
*/
redirect?: string;
/**
* The event emitted by this method. Can be set to `null` to skip event emitting.
*/
Expand Down
15 changes: 10 additions & 5 deletions packages/koa/src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ export function rest () {
route
};
const args = createArguments({ id, data, params });
const hookContext = createContext(service, method);
const contextBase = createContext(service, method);
ctx.hook = contextBase;

ctx.hook = hookContext as any;
const context = await (service as any)[method](...args, contextBase);
ctx.hook = context;

const result = await (service as any)[method](...args, hookContext);
const result = http.getData(context);
const statusCode = http.getStatusCode(context, result);
const location = http.getLocation(context, ctx.get('Referrer'));

ctx.response.status = http.getStatusCode(result, {});
ctx.body = http.getData(result);
ctx.body = result;
ctx.status = statusCode;
if (location) ctx.set('Location', location);
}

return next();
Expand Down
2 changes: 2 additions & 0 deletions packages/transport-commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@
"@feathersjs/commons": "^5.0.0-pre.14",
"@feathersjs/errors": "^5.0.0-pre.14",
"@feathersjs/feathers": "^5.0.0-pre.14",
"encodeurl": "^1.0.2",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/encodeurl": "^1.0.0",
"@types/lodash": "^4.14.176",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.6",
Expand Down
18 changes: 17 additions & 1 deletion packages/transport-commons/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MethodNotAllowed } from '@feathersjs/errors/lib';
import { HookContext, NullableId, Params } from '@feathersjs/feathers';
import encodeUrl from 'encodeurl';

export const METHOD_HEADER = 'x-service-method';

Expand All @@ -13,7 +14,8 @@ export const statusCodes = {
created: 201,
noContent: 204,
methodNotAllowed: 405,
success: 200
success: 200,
seeOther: 303
};

export const knownMethods: { [key: string]: string } = {
Expand Down Expand Up @@ -68,9 +70,23 @@ export function getStatusCode (context: HookContext, data?: any) {
return statusCodes.created;
}

if (context.redirect) {
return statusCodes.seeOther;
}

if (!data) {
return statusCodes.noContent;
}

return statusCodes.success;
}

export function getLocation(context: HookContext, referrer?: string) {
const redirect = context.redirect;

if (redirect === 'back') {
return encodeUrl(referrer || '/');
}

return redirect && encodeUrl(redirect);
}
0