Add Saleor specific middlewares
This commit is contained in:
parent
3f4fdb5f57
commit
9da7c92642
4 changed files with 94 additions and 8 deletions
18
package.json
18
package.json
|
@ -17,14 +17,28 @@
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fast-glob": "^3.2.11",
|
"fast-glob": "^3.2.11",
|
||||||
"graphql": "^16.5.0"
|
"graphql": "^16.5.0",
|
||||||
|
"retes": "^0.26.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^17.0.35",
|
"@types/node": "^17.0.35",
|
||||||
"tsm": "^2.2.1",
|
"tsm": "^2.2.1",
|
||||||
"tsup": "^5.12.8",
|
"tsup": "^6.0.1",
|
||||||
"typescript": "^4.7.2",
|
"typescript": "^4.7.2",
|
||||||
"uvu": "^0.5.3",
|
"uvu": "^0.5.3",
|
||||||
"watchlist": "^0.3.1"
|
"watchlist": "^0.3.1"
|
||||||
|
},
|
||||||
|
"exports": {
|
||||||
|
"./package.json": "./package.json",
|
||||||
|
"./middleware": {
|
||||||
|
"types": "./middleware.d.ts",
|
||||||
|
"import": "./middleware.mjs",
|
||||||
|
"require": "./middleware.js"
|
||||||
|
},
|
||||||
|
".": {
|
||||||
|
"types": "./index.d.ts",
|
||||||
|
"import": "./index.mjs",
|
||||||
|
"require": "./index.js"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,9 @@ specifiers:
|
||||||
'@types/node': ^17.0.35
|
'@types/node': ^17.0.35
|
||||||
fast-glob: ^3.2.11
|
fast-glob: ^3.2.11
|
||||||
graphql: ^16.5.0
|
graphql: ^16.5.0
|
||||||
|
retes: ^0.26.0
|
||||||
tsm: ^2.2.1
|
tsm: ^2.2.1
|
||||||
tsup: ^5.12.8
|
tsup: ^6.0.1
|
||||||
typescript: ^4.7.2
|
typescript: ^4.7.2
|
||||||
uvu: ^0.5.3
|
uvu: ^0.5.3
|
||||||
watchlist: ^0.3.1
|
watchlist: ^0.3.1
|
||||||
|
@ -13,12 +14,13 @@ specifiers:
|
||||||
dependencies:
|
dependencies:
|
||||||
fast-glob: 3.2.11
|
fast-glob: 3.2.11
|
||||||
graphql: 16.5.0
|
graphql: 16.5.0
|
||||||
typescript: 4.7.2
|
retes: 0.26.0
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@types/node': 17.0.35
|
'@types/node': 17.0.35
|
||||||
tsm: 2.2.1
|
tsm: 2.2.1
|
||||||
tsup: 5.12.8_typescript@4.7.2
|
tsup: 6.0.1_typescript@4.7.2
|
||||||
|
typescript: 4.7.2
|
||||||
uvu: 0.5.3
|
uvu: 0.5.3
|
||||||
watchlist: 0.3.1
|
watchlist: 0.3.1
|
||||||
|
|
||||||
|
@ -95,6 +97,13 @@ packages:
|
||||||
load-tsconfig: 0.2.3
|
load-tsconfig: 0.2.3
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/busboy/1.6.0:
|
||||||
|
resolution: {integrity: sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==}
|
||||||
|
engines: {node: '>=10.16.0'}
|
||||||
|
dependencies:
|
||||||
|
streamsearch: 1.1.0
|
||||||
|
dev: false
|
||||||
|
|
||||||
/cac/6.7.12:
|
/cac/6.7.12:
|
||||||
resolution: {integrity: sha512-rM7E2ygtMkJqD9c7WnFU6fruFcN3xe4FM5yUmgxhZzIKJk4uHl9U/fhwdajGFQbQuv43FAUo1Fe8gX/oIKDeSA==}
|
resolution: {integrity: sha512-rM7E2ygtMkJqD9c7WnFU6fruFcN3xe4FM5yUmgxhZzIKJk4uHl9U/fhwdajGFQbQuv43FAUo1Fe8gX/oIKDeSA==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
|
@ -669,6 +678,12 @@ packages:
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/retes/0.26.0:
|
||||||
|
resolution: {integrity: sha512-hF8B2/ccHysMhBrYAkzOGWK2OmkFR/7efkbB0G3Hj7ePDUdKv0Ki8iQjoJOPkw+q2QIQ8fbPVY5VC2OwheNODg==}
|
||||||
|
dependencies:
|
||||||
|
busboy: 1.6.0
|
||||||
|
dev: false
|
||||||
|
|
||||||
/reusify/1.0.4:
|
/reusify/1.0.4:
|
||||||
resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==}
|
resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==}
|
||||||
engines: {iojs: '>=1.0.0', node: '>=0.10.0'}
|
engines: {iojs: '>=1.0.0', node: '>=0.10.0'}
|
||||||
|
@ -721,6 +736,11 @@ packages:
|
||||||
whatwg-url: 7.1.0
|
whatwg-url: 7.1.0
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/streamsearch/1.1.0:
|
||||||
|
resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==}
|
||||||
|
engines: {node: '>=10.0.0'}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/strip-final-newline/2.0.0:
|
/strip-final-newline/2.0.0:
|
||||||
resolution: {integrity: sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==}
|
resolution: {integrity: sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==}
|
||||||
engines: {node: '>=6'}
|
engines: {node: '>=6'}
|
||||||
|
@ -781,12 +801,19 @@ packages:
|
||||||
esbuild: 0.14.39
|
esbuild: 0.14.39
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/tsup/5.12.8_typescript@4.7.2:
|
/tsup/6.0.1_typescript@4.7.2:
|
||||||
resolution: {integrity: sha512-fSBzUBtrnAQ+XKPfj1KcZ2Pl0EUlKBqpbTmRAs+2mL34fQlowrm6ccQgOYyMe9MMAcejMP6/7Rw3qKjx1lreZA==}
|
resolution: {integrity: sha512-2Pl1YkEEvzfg4aYgwUILaHV0wwZQKO7mNHMQChyzdCNlQqf11eug7gWk86XCb3CvRofCXBX87x73BTGjyS3cOQ==}
|
||||||
|
engines: {node: '>=14'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
|
'@swc/core': ^1
|
||||||
|
postcss: ^8.4.12
|
||||||
typescript: ^4.1.0
|
typescript: ^4.1.0
|
||||||
peerDependenciesMeta:
|
peerDependenciesMeta:
|
||||||
|
'@swc/core':
|
||||||
|
optional: true
|
||||||
|
postcss:
|
||||||
|
optional: true
|
||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
dependencies:
|
dependencies:
|
||||||
|
@ -806,7 +833,6 @@ packages:
|
||||||
tree-kill: 1.2.2
|
tree-kill: 1.2.2
|
||||||
typescript: 4.7.2
|
typescript: 4.7.2
|
||||||
transitivePeerDependencies:
|
transitivePeerDependencies:
|
||||||
- postcss
|
|
||||||
- supports-color
|
- supports-color
|
||||||
- ts-node
|
- ts-node
|
||||||
dev: true
|
dev: true
|
||||||
|
@ -815,6 +841,7 @@ packages:
|
||||||
resolution: {integrity: sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==}
|
resolution: {integrity: sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==}
|
||||||
engines: {node: '>=4.2.0'}
|
engines: {node: '>=4.2.0'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
dev: true
|
||||||
|
|
||||||
/uvu/0.5.3:
|
/uvu/0.5.3:
|
||||||
resolution: {integrity: sha512-brFwqA3FXzilmtnIyJ+CxdkInkY/i4ErvP7uV0DnUVxQcQ55reuHphorpF+tZoVHK2MniZ/VJzI7zJQoc9T9Yw==}
|
resolution: {integrity: sha512-brFwqA3FXzilmtnIyJ+CxdkInkY/i4ErvP7uV0DnUVxQcQ55reuHphorpF+tZoVHK2MniZ/VJzI7zJQoc9T9Yw==}
|
||||||
|
|
2
src/const.ts
Normal file
2
src/const.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const SALEOR_DOMAIN_HEADER = "saleor-domain";
|
||||||
|
export const SALEOR_EVENT_HEADER = "saleor-event";
|
43
src/middleware.ts
Normal file
43
src/middleware.ts
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import type { Middleware } from "retes";
|
||||||
|
|
||||||
|
import { Response } from 'retes/response';
|
||||||
|
|
||||||
|
import * as Const from './const';
|
||||||
|
|
||||||
|
export const withBaseURL: Middleware = (handler) => async (request) => {
|
||||||
|
const { host, "x-forwarded-proto": protocol = "http" } = request.headers;
|
||||||
|
|
||||||
|
request.context.baseURL = `${protocol}://${host}`;
|
||||||
|
|
||||||
|
const response = await handler(request);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const withSaleorDomainPresent: Middleware = (handler) => async (request) => {
|
||||||
|
const saleor_domain = request.headers[Const.SALEOR_DOMAIN_HEADER];
|
||||||
|
|
||||||
|
if (!saleor_domain) {
|
||||||
|
return Response.BadRequest({ success: false, message: "Missing Saleor domain header." });
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler(request);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const withSaleorEventMatch = (expectedEvent: string): Middleware => (handler) => async (request) => {
|
||||||
|
const receivedEvent = request.headers[Const.SALEOR_EVENT_HEADER];
|
||||||
|
|
||||||
|
if (receivedEvent !== expectedEvent) {
|
||||||
|
return Response.BadRequest({ success: false, message: "Invalid Saleor Event" });
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler(request);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const withAuthTokenRequired: Middleware = (handler) => async (request) => {
|
||||||
|
const auth_token = request.params.auth_token;
|
||||||
|
if (!auth_token) {
|
||||||
|
return Response.BadRequest({ success: false, message: "Missing auth token." });
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler(request);
|
||||||
|
};
|
Loading…
Reference in a new issue