Skip to content

Commit 8ffce9e

Browse files
committed
feat(http): Add redirect property to hook context
1 parent 12b2ae8 commit 8ffce9e

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

packages/express/src/rest.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ export const serviceMiddleware = (callback: ServiceCallback) =>
4040
const { query, body: data } = req;
4141
const { __feathersId: id = null, ...route } = req.params;
4242
const params = { query, route, ...req.feathers };
43+
4344
const context = await callback(req, res, { id, data, params });
45+
4446
const result = http.getData(context);
47+
const statusCode = http.getStatusCode(context, result);
48+
const location = http.getLocation(context, req.get('Referrer'));
4549

4650
res.data = result;
47-
res.status(http.getStatusCode(context, result));
51+
res.status(statusCode);
52+
if (location) res.set('Location', location);
4853

4954
next();
5055
} catch (error: any) {
@@ -66,11 +71,13 @@ export const serviceMethodHandler = (
6671
}
6772

6873
const args = getArgs(options);
69-
const context = createContext(service, method);
74+
const contextBase = createContext(service, method);
75+
res.hook = contextBase;
7076

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

73-
return service[method](...args, context);
80+
return context;
7481
});
7582

7683
export function rest (handler: RequestHandler = formatter) {

packages/feathers/src/declarations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
312312
* code that should be returned.
313313
*/
314314
statusCode?: number;
315+
/**
316+
* A writeable, optional property that allows to set a redirect location.
317+
*/
318+
redirect?: string;
315319
/**
316320
* The event emitted by this method. Can be set to `null` to skip event emitting.
317321
*/

packages/koa/src/rest.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ export function rest () {
3737
route
3838
};
3939
const args = createArguments({ id, data, params });
40-
const hookContext = createContext(service, method);
40+
const contextBase = createContext(service, method);
41+
ctx.hook = contextBase;
4142

42-
ctx.hook = hookContext as any;
43+
const context = await (service as any)[method](...args, contextBase);
44+
ctx.hook = context;
4345

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

46-
ctx.response.status = http.getStatusCode(result, {});
47-
ctx.body = http.getData(result);
50+
ctx.body = result;
51+
ctx.status = statusCode;
52+
if (location) ctx.set('Location', location);
4853
}
4954

5055
return next();

packages/transport-commons/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555
"@feathersjs/commons": "^5.0.0-pre.14",
5656
"@feathersjs/errors": "^5.0.0-pre.14",
5757
"@feathersjs/feathers": "^5.0.0-pre.14",
58+
"encodeurl": "^1.0.2",
5859
"lodash": "^4.17.21"
5960
},
6061
"devDependencies": {
62+
"@types/encodeurl": "^1.0.0",
6163
"@types/lodash": "^4.14.176",
6264
"@types/mocha": "^9.0.0",
6365
"@types/node": "^16.11.6",

packages/transport-commons/src/http.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MethodNotAllowed } from '@feathersjs/errors/lib';
22
import { HookContext, NullableId, Params } from '@feathersjs/feathers';
3+
import encodeUrl from 'encodeurl';
34

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

@@ -13,7 +14,8 @@ export const statusCodes = {
1314
created: 201,
1415
noContent: 204,
1516
methodNotAllowed: 405,
16-
success: 200
17+
success: 200,
18+
seeOther: 303
1719
};
1820

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

73+
if (context.redirect) {
74+
return statusCodes.seeOther;
75+
}
76+
7177
if (!data) {
7278
return statusCodes.noContent;
7379
}
7480

7581
return statusCodes.success;
7682
}
83+
84+
export function getLocation(context: HookContext, referrer?: string) {
85+
const redirect = context.redirect;
86+
87+
if (redirect === 'back') {
88+
return encodeUrl(referrer || '/');
89+
}
90+
91+
return redirect && encodeUrl(redirect);
92+
}

0 commit comments

Comments
 (0)