@unchainedshop/api 3.0.0-alpha3 → 3.0.0-alpha4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/express/index.d.ts +2 -2
- package/lib/express/index.d.ts.map +1 -1
- package/lib/express/index.js.map +1 -1
- package/lib/fastify/index.d.ts +9 -0
- package/lib/fastify/index.d.ts.map +1 -0
- package/lib/fastify/index.js +99 -0
- package/lib/fastify/index.js.map +1 -0
- package/lib/resolvers/index.js +6 -6
- package/lib/resolvers/index.js.map +1 -1
- package/package.json +14 -11
- package/src/express/index.ts +2 -2
- package/src/fastify/index.ts +128 -0
- package/src/resolvers/index.ts +10 -10
- package/tests/api-index.test.ts +35 -33
package/lib/express/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type e from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import { YogaServerInstance } from 'graphql-yoga';
|
|
3
3
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
4
4
|
import { UnchainedCore } from '@unchainedshop/core';
|
|
5
5
|
export declare const connect: (expressApp: e.Express, { graphqlHandler, db, unchainedAPI, }: {
|
|
6
|
-
graphqlHandler:
|
|
6
|
+
graphqlHandler: YogaServerInstance<any, any>;
|
|
7
7
|
db: mongodb.Db;
|
|
8
8
|
unchainedAPI: UnchainedCore;
|
|
9
9
|
}) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/express/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAM7B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/express/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAM7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,OAAO,EAAE,MAAM,wB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AY;YACvB,cAAc,EAAE,UAAU;SAC3B,CAAC;QACF,IAAI,EAAE,qBAAqB;QAC3B,iBAAiB,EAAE,KAAK;QACxB,MAAM,EAAE,KAAK;QACb,MAAM,EAAE;YACN,MAAM,EAAE,uBAAuB;YAC/B,QAAQ,EAAE,OAAO,CAAC,QAAQ,KAAK,YAAY,CAAC;YAC5C,IAAI,EAAE,qBAAqB;YAC3B,QAAQ,EAAE,KAAK;YACf,MAAM,EAAE,QAAQ,KAAK,YAAY;YACjC,MAAM,EAAE,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;SAChC;KACF,CAAC,EACF,QAAQ,CAAC,UAAU,EAAE,EACrB,QAAQ,CAAC,OAAO,EAAE,EAClB,QAAQ,CAAC,YAAY,CAAC,cAAc,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,EACzD,UAAU,CACX,CAAC;IACF,UAAU,CAAC,GAAG,CAAC,gBAAgB,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxD,UAAU,CAAC,GAAG,CAAC,qBAAqB,EAAE,2BAA2B,CAAC,CAAC;IACnE,UAAU,CAAC,GAAG,CAAC,oBAAoB,EAAE,0BAA0B,CAAC,CAAC;AACnE,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { YogaServerInstance } from 'graphql-yoga';
|
|
2
|
+
import { mongodb } from '@unchainedshop/mongodb';
|
|
3
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
4
|
+
export declare const connect: (fastify: any, { graphqlHandler, db, }: {
|
|
5
|
+
graphqlHandler: YogaServerInstance<any, any>;
|
|
6
|
+
db: mongodb.Db;
|
|
7
|
+
unchainedAPI: UnchainedCore;
|
|
8
|
+
}) => void;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fastify/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAyEpD,eAAO,MAAM,OAAO,YACT,GAAG,2BAIT;IAAE,cAAc,EAAE,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAAC,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC;IAAC,YAAY,EAAE,aAAa,CAAA;CAAE,SA2CjG,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { getCurrentContextResolver } from '../context.js';
|
|
2
|
+
// import createBulkImportMiddleware from './createBulkImportMiddleware.js';
|
|
3
|
+
// import createERCMetadataMiddleware from './createERCMetadataMiddleware.js';
|
|
4
|
+
import MongoStore from 'connect-mongo';
|
|
5
|
+
import { emit } from '@unchainedshop/events';
|
|
6
|
+
import { API_EVENTS } from '../events.js';
|
|
7
|
+
import fastifySession from '@fastify/session';
|
|
8
|
+
import fastifyCookie from '@fastify/cookie';
|
|
9
|
+
const resolveUserRemoteAddress = (req) => {
|
|
10
|
+
const remoteAddress = req.headers['x-real-ip'] ||
|
|
11
|
+
req.headers['x-forwarded-for'] ||
|
|
12
|
+
req.socket?.remoteAddress;
|
|
13
|
+
const remotePort = req.socket?.remotePort;
|
|
14
|
+
return { remoteAddress, remotePort };
|
|
15
|
+
};
|
|
16
|
+
const { GRAPHQL_API_PATH = '/graphql', UNCHAINED_COOKIE_NAME = 'unchained_token', UNCHAINED_COOKIE_PATH = '/', UNCHAINED_COOKIE_DOMAIN, NODE_ENV, } = process.env;
|
|
17
|
+
const middlewareHook = async function middlewareHook(req, reply) {
|
|
18
|
+
const setHeader = (key, value) => reply.header(key, value);
|
|
19
|
+
const getHeader = (key) => req.headers[key];
|
|
20
|
+
const { remoteAddress, remotePort } = resolveUserRemoteAddress(req);
|
|
21
|
+
const context = getCurrentContextResolver();
|
|
22
|
+
async function login(user) {
|
|
23
|
+
req.session.user = user;
|
|
24
|
+
req.session.userId = user._id;
|
|
25
|
+
const tokenObject = {
|
|
26
|
+
_id: req.session.sessionId,
|
|
27
|
+
/* eslint-disable-next-line */
|
|
28
|
+
tokenExpires: new Date(req.session?.cookie._expires),
|
|
29
|
+
};
|
|
30
|
+
await emit(API_EVENTS.API_LOGIN_TOKEN_CREATED, { userId: user._id, ...tokenObject });
|
|
31
|
+
/* eslint-disable-next-line */
|
|
32
|
+
user._inLoginMethodResponse = true;
|
|
33
|
+
return { user, ...tokenObject };
|
|
34
|
+
}
|
|
35
|
+
async function logout() {
|
|
36
|
+
/* eslint-disable-line */
|
|
37
|
+
if (!req.session?.userId)
|
|
38
|
+
return false;
|
|
39
|
+
const tokenObject = {
|
|
40
|
+
_id: req.session.sessionId,
|
|
41
|
+
userId: req.session?.userId,
|
|
42
|
+
};
|
|
43
|
+
req.session.user = null;
|
|
44
|
+
req.session.userId = null;
|
|
45
|
+
await emit(API_EVENTS.API_LOGOUT, tokenObject);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
req.unchainedContext = await context({
|
|
49
|
+
setHeader,
|
|
50
|
+
getHeader,
|
|
51
|
+
remoteAddress,
|
|
52
|
+
remotePort,
|
|
53
|
+
login,
|
|
54
|
+
logout,
|
|
55
|
+
user: req.session.user,
|
|
56
|
+
userId: req.session.userId,
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
export const connect = (fastify, { graphqlHandler, db, }) => {
|
|
60
|
+
fastify.register(fastifyCookie);
|
|
61
|
+
fastify.register(fastifySession, {
|
|
62
|
+
secret: process.env.UNCHAINED_TOKEN_SECRET,
|
|
63
|
+
cookieName: UNCHAINED_COOKIE_NAME,
|
|
64
|
+
store: MongoStore.create({
|
|
65
|
+
client: db.client,
|
|
66
|
+
dbName: db.databaseName,
|
|
67
|
+
collectionName: 'sessions',
|
|
68
|
+
}),
|
|
69
|
+
cookie: {
|
|
70
|
+
domain: UNCHAINED_COOKIE_DOMAIN,
|
|
71
|
+
httpOnly: Boolean(NODE_ENV === 'production'),
|
|
72
|
+
path: UNCHAINED_COOKIE_PATH,
|
|
73
|
+
secure: NODE_ENV === 'production',
|
|
74
|
+
maxAge: 1000 * 60 * 60 * 24 * 7,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
fastify.decorateRequest('unchainedContext');
|
|
78
|
+
fastify.addHook('onRequest', middlewareHook);
|
|
79
|
+
fastify.route({
|
|
80
|
+
url: GRAPHQL_API_PATH,
|
|
81
|
+
method: ['GET', 'POST', 'OPTIONS'],
|
|
82
|
+
handler: async (req, reply) => {
|
|
83
|
+
// Second parameter adds Fastify's `req` and `reply` to the GraphQL Context
|
|
84
|
+
const response = await graphqlHandler.handleNodeRequestAndResponse(req, reply, {
|
|
85
|
+
req,
|
|
86
|
+
reply,
|
|
87
|
+
});
|
|
88
|
+
response.headers.forEach((value, key) => {
|
|
89
|
+
reply.header(key, value);
|
|
90
|
+
});
|
|
91
|
+
reply.status(response.status);
|
|
92
|
+
reply.send(response.body);
|
|
93
|
+
return reply;
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
// expressApp.use(ERC_METADATA_API_PATH, createERCMetadataMiddleware);
|
|
97
|
+
// expressApp.use(BULK_IMPORT_API_PATH, createBulkImportMiddleware);
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fastify/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAC1D,4EAA4E;AAC5E,8EAA8E;AAC9E,OAAO,UAAU,MAAM,eAAe,CAAC;AAIvC,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C,OAAO,cAAc,MAAM,kBAAkB,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AC;IAEH,wEAAwE;IACxE,sEAAsE;AACxE,CAAC,CAAC"}
|
package/lib/resolvers/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GraphQLJSON, GraphQLTimestamp, GraphQLDateTimeISO, GraphQLDate, GraphQLLocale, } from 'graphql-scalars';
|
|
2
2
|
import Query from './queries/index.js';
|
|
3
3
|
import Mutation from './mutations/index.js';
|
|
4
4
|
import Types from './type/index.js';
|
|
@@ -6,12 +6,12 @@ import CustomScalars from './scalars/index.js';
|
|
|
6
6
|
export default {
|
|
7
7
|
...Types,
|
|
8
8
|
...CustomScalars,
|
|
9
|
-
JSON:
|
|
9
|
+
JSON: GraphQLJSON,
|
|
10
10
|
Query,
|
|
11
11
|
Mutation,
|
|
12
|
-
DateTime:
|
|
13
|
-
Date:
|
|
14
|
-
Timestamp:
|
|
15
|
-
Locale:
|
|
12
|
+
DateTime: GraphQLDateTimeISO,
|
|
13
|
+
Date: GraphQLDate,
|
|
14
|
+
Timestamp: GraphQLTimestamp,
|
|
15
|
+
Locale: GraphQLLocale,
|
|
16
16
|
};
|
|
17
17
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/resolvers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/resolvers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,WAAW,EACX,aAAa,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,MAAM,oBAAoB,CAAC;AACvC,OAAO,QAAQ,MAAM,sBAAsB,CAAC;AAC5C,OAAO,KAAK,MAAM,iBAAiB,CAAC;AACpC,OAAO,aAAa,MAAM,oBAAoB,CAAC;AAE/C,eAAe;IACb,GAAG,KAAK;IACR,GAAG,aAAa;IAChB,IAAI,EAAE,WAAW;IACjB,KAAK;IACL,QAAQ;IACR,QAAQ,EAAE,kBAAkB;IAC5B,IAAI,EAAE,WAAW;IACjB,SAAS,EAAE,gBAAgB;IAC3B,MAAM,EAAE,aAAa;CACY,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/api",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-alpha4",
|
|
4
4
|
"main": "lib/api-index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/api-index.js",
|
|
@@ -43,34 +43,37 @@
|
|
|
43
43
|
"graphql-yoga": "^5.7.0"
|
|
44
44
|
},
|
|
45
45
|
"optionalDependencies": {
|
|
46
|
+
"@fastify/cookie": "^11.0.1",
|
|
47
|
+
"@fastify/session": "^11.0.1",
|
|
46
48
|
"connect-mongo": "~5",
|
|
47
|
-
"cookie-parser": "^1.4.
|
|
49
|
+
"cookie-parser": "^1.4.7",
|
|
48
50
|
"express": "~4",
|
|
49
51
|
"express-session": "~1",
|
|
52
|
+
"fastify": "^5.0.0",
|
|
50
53
|
"passport": "^0.7.0",
|
|
51
54
|
"passport-strategy": "^1.0.0"
|
|
52
55
|
},
|
|
53
56
|
"dependencies": {
|
|
54
57
|
"@metamask/eth-sig-util": "^7.0.3",
|
|
55
|
-
"@unchainedshop/core": "^3.0.0-
|
|
56
|
-
"@unchainedshop/events": "^3.0.0-
|
|
57
|
-
"@unchainedshop/logger": "^3.0.0-
|
|
58
|
-
"@unchainedshop/roles": "^3.0.0-
|
|
59
|
-
"@unchainedshop/utils": "^3.0.0-
|
|
58
|
+
"@unchainedshop/core": "^3.0.0-alpha4",
|
|
59
|
+
"@unchainedshop/events": "^3.0.0-alpha4",
|
|
60
|
+
"@unchainedshop/logger": "^3.0.0-alpha4",
|
|
61
|
+
"@unchainedshop/roles": "^3.0.0-alpha4",
|
|
62
|
+
"@unchainedshop/utils": "^3.0.0-alpha4",
|
|
60
63
|
"accounting": "0.4.1",
|
|
61
64
|
"dataloader": "^2.2.2",
|
|
62
65
|
"graphql-scalars": "^1.23.0",
|
|
63
66
|
"memoizee": "^0.4.17"
|
|
64
67
|
},
|
|
65
68
|
"devDependencies": {
|
|
66
|
-
"@types/node": "^22.
|
|
69
|
+
"@types/node": "^22.7.8",
|
|
67
70
|
"connect-mongo": "^5.1.0",
|
|
68
|
-
"express": "^4.21.
|
|
69
|
-
"express-session": "^1.18.
|
|
71
|
+
"express": "^4.21.1",
|
|
72
|
+
"express-session": "^1.18.1",
|
|
70
73
|
"jest": "^29.7.0",
|
|
71
74
|
"passport": "^0.7.0",
|
|
72
75
|
"passport-strategy": "^1.0.0",
|
|
73
76
|
"ts-jest": "^29.2.5",
|
|
74
|
-
"typescript": "^5.6.
|
|
77
|
+
"typescript": "^5.6.3"
|
|
75
78
|
}
|
|
76
79
|
}
|
package/src/express/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import createBulkImportMiddleware from './createBulkImportMiddleware.js';
|
|
|
4
4
|
import createERCMetadataMiddleware from './createERCMetadataMiddleware.js';
|
|
5
5
|
import session from 'express-session';
|
|
6
6
|
import MongoStore from 'connect-mongo';
|
|
7
|
-
import {
|
|
7
|
+
import { YogaServerInstance } from 'graphql-yoga';
|
|
8
8
|
import setupPassport from './passport/setup.js';
|
|
9
9
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
10
10
|
import { UnchainedCore } from '@unchainedshop/core';
|
|
@@ -116,7 +116,7 @@ export const connect = (
|
|
|
116
116
|
graphqlHandler,
|
|
117
117
|
db,
|
|
118
118
|
unchainedAPI,
|
|
119
|
-
}: { graphqlHandler:
|
|
119
|
+
}: { graphqlHandler: YogaServerInstance<any, any>; db: mongodb.Db; unchainedAPI: UnchainedCore },
|
|
120
120
|
) => {
|
|
121
121
|
const passport = setupPassport(unchainedAPI);
|
|
122
122
|
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { getCurrentContextResolver } from '../context.js';
|
|
2
|
+
// import createBulkImportMiddleware from './createBulkImportMiddleware.js';
|
|
3
|
+
// import createERCMetadataMiddleware from './createERCMetadataMiddleware.js';
|
|
4
|
+
import MongoStore from 'connect-mongo';
|
|
5
|
+
import { YogaServerInstance } from 'graphql-yoga';
|
|
6
|
+
import { mongodb } from '@unchainedshop/mongodb';
|
|
7
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
8
|
+
import { emit } from '@unchainedshop/events';
|
|
9
|
+
import { API_EVENTS } from '../events.js';
|
|
10
|
+
import { User } from '@unchainedshop/core-users';
|
|
11
|
+
import { IncomingMessage } from 'http';
|
|
12
|
+
import fastifySession from '@fastify/session';
|
|
13
|
+
import fastifyCookie from '@fastify/cookie';
|
|
14
|
+
|
|
15
|
+
const resolveUserRemoteAddress = (req: IncomingMessage) => {
|
|
16
|
+
const remoteAddress =
|
|
17
|
+
(req.headers['x-real-ip'] as string) ||
|
|
18
|
+
(req.headers['x-forwarded-for'] as string) ||
|
|
19
|
+
req.socket?.remoteAddress;
|
|
20
|
+
|
|
21
|
+
const remotePort = req.socket?.remotePort;
|
|
22
|
+
|
|
23
|
+
return { remoteAddress, remotePort };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
GRAPHQL_API_PATH = '/graphql',
|
|
28
|
+
UNCHAINED_COOKIE_NAME = 'unchained_token',
|
|
29
|
+
UNCHAINED_COOKIE_PATH = '/',
|
|
30
|
+
UNCHAINED_COOKIE_DOMAIN,
|
|
31
|
+
NODE_ENV,
|
|
32
|
+
} = process.env;
|
|
33
|
+
|
|
34
|
+
const middlewareHook = async function middlewareHook(req: any, reply: any) {
|
|
35
|
+
const setHeader = (key, value) => reply.header(key, value);
|
|
36
|
+
const getHeader = (key) => req.headers[key];
|
|
37
|
+
const { remoteAddress, remotePort } = resolveUserRemoteAddress(req);
|
|
38
|
+
|
|
39
|
+
const context = getCurrentContextResolver();
|
|
40
|
+
|
|
41
|
+
async function login(user: User) {
|
|
42
|
+
req.session.user = user;
|
|
43
|
+
req.session.userId = user._id;
|
|
44
|
+
const tokenObject = {
|
|
45
|
+
_id: req.session.sessionId,
|
|
46
|
+
/* eslint-disable-next-line */
|
|
47
|
+
tokenExpires: new Date((req as any).session?.cookie._expires),
|
|
48
|
+
};
|
|
49
|
+
await emit(API_EVENTS.API_LOGIN_TOKEN_CREATED, { userId: user._id, ...tokenObject });
|
|
50
|
+
/* eslint-disable-next-line */
|
|
51
|
+
(user as any)._inLoginMethodResponse = true;
|
|
52
|
+
return { user, ...tokenObject };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function logout() {
|
|
56
|
+
/* eslint-disable-line */
|
|
57
|
+
if (!req.session?.userId) return false;
|
|
58
|
+
const tokenObject = {
|
|
59
|
+
_id: (req as any).session.sessionId,
|
|
60
|
+
userId: req.session?.userId,
|
|
61
|
+
};
|
|
62
|
+
req.session.user = null;
|
|
63
|
+
req.session.userId = null;
|
|
64
|
+
await emit(API_EVENTS.API_LOGOUT, tokenObject);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
(req as any).unchainedContext = await context({
|
|
69
|
+
setHeader,
|
|
70
|
+
getHeader,
|
|
71
|
+
remoteAddress,
|
|
72
|
+
remotePort,
|
|
73
|
+
login,
|
|
74
|
+
logout,
|
|
75
|
+
user: req.session.user,
|
|
76
|
+
userId: req.session.userId,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const connect = (
|
|
81
|
+
fastify: any,
|
|
82
|
+
{
|
|
83
|
+
graphqlHandler,
|
|
84
|
+
db,
|
|
85
|
+
}: { graphqlHandler: YogaServerInstance<any, any>; db: mongodb.Db; unchainedAPI: UnchainedCore },
|
|
86
|
+
) => {
|
|
87
|
+
fastify.register(fastifyCookie);
|
|
88
|
+
fastify.register(fastifySession, {
|
|
89
|
+
secret: process.env.UNCHAINED_TOKEN_SECRET,
|
|
90
|
+
cookieName: UNCHAINED_COOKIE_NAME,
|
|
91
|
+
store: MongoStore.create({
|
|
92
|
+
client: (db as any).client,
|
|
93
|
+
dbName: db.databaseName,
|
|
94
|
+
collectionName: 'sessions',
|
|
95
|
+
}),
|
|
96
|
+
cookie: {
|
|
97
|
+
domain: UNCHAINED_COOKIE_DOMAIN,
|
|
98
|
+
httpOnly: Boolean(NODE_ENV === 'production'),
|
|
99
|
+
path: UNCHAINED_COOKIE_PATH,
|
|
100
|
+
secure: NODE_ENV === 'production',
|
|
101
|
+
maxAge: 1000 * 60 * 60 * 24 * 7,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
fastify.decorateRequest('unchainedContext');
|
|
106
|
+
fastify.addHook('onRequest', middlewareHook);
|
|
107
|
+
|
|
108
|
+
fastify.route({
|
|
109
|
+
url: GRAPHQL_API_PATH,
|
|
110
|
+
method: ['GET', 'POST', 'OPTIONS'],
|
|
111
|
+
handler: async (req, reply) => {
|
|
112
|
+
// Second parameter adds Fastify's `req` and `reply` to the GraphQL Context
|
|
113
|
+
const response = await graphqlHandler.handleNodeRequestAndResponse(req, reply, {
|
|
114
|
+
req,
|
|
115
|
+
reply,
|
|
116
|
+
});
|
|
117
|
+
response.headers.forEach((value, key) => {
|
|
118
|
+
reply.header(key, value);
|
|
119
|
+
});
|
|
120
|
+
reply.status(response.status);
|
|
121
|
+
reply.send(response.body);
|
|
122
|
+
return reply;
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// expressApp.use(ERC_METADATA_API_PATH, createERCMetadataMiddleware);
|
|
127
|
+
// expressApp.use(BULK_IMPORT_API_PATH, createBulkImportMiddleware);
|
|
128
|
+
};
|
package/src/resolvers/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
GraphQLJSON,
|
|
3
|
+
GraphQLTimestamp,
|
|
4
|
+
GraphQLDateTimeISO,
|
|
5
|
+
GraphQLDate,
|
|
6
|
+
GraphQLLocale,
|
|
7
7
|
} from 'graphql-scalars';
|
|
8
8
|
import Query from './queries/index.js';
|
|
9
9
|
import Mutation from './mutations/index.js';
|
|
@@ -13,11 +13,11 @@ import CustomScalars from './scalars/index.js';
|
|
|
13
13
|
export default {
|
|
14
14
|
...Types,
|
|
15
15
|
...CustomScalars,
|
|
16
|
-
JSON:
|
|
16
|
+
JSON: GraphQLJSON,
|
|
17
17
|
Query,
|
|
18
18
|
Mutation,
|
|
19
|
-
DateTime:
|
|
20
|
-
Date:
|
|
21
|
-
Timestamp:
|
|
22
|
-
Locale:
|
|
19
|
+
DateTime: GraphQLDateTimeISO,
|
|
20
|
+
Date: GraphQLDate,
|
|
21
|
+
Timestamp: GraphQLTimestamp,
|
|
22
|
+
Locale: GraphQLLocale,
|
|
23
23
|
} as unknown as Record<string, any>;
|
package/tests/api-index.test.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
// Import the function to be tested.
|
|
2
|
-
import {admin} from '../lib/roles/admin.js'
|
|
3
|
-
import {actions} from '../lib/roles/index.js';
|
|
4
|
-
import {checkAction, ensureActionExists, ensureIsFunction} from '../lib/acl.js'
|
|
2
|
+
import { admin } from '../lib/roles/admin.js';
|
|
3
|
+
import { actions } from '../lib/roles/index.js';
|
|
4
|
+
import { checkAction, ensureActionExists, ensureIsFunction } from '../lib/acl.js';
|
|
5
5
|
import { NoPermissionError, PermissionSystemError } from '../lib/errors.js';
|
|
6
6
|
import { Roles } from '@unchainedshop/roles';
|
|
7
7
|
|
|
8
8
|
describe('API', () => {
|
|
9
|
-
|
|
10
9
|
describe('roles', () => {
|
|
11
10
|
const role = {
|
|
12
|
-
allow: import.meta.jest.fn()
|
|
11
|
+
allow: import.meta.jest.fn(),
|
|
13
12
|
};
|
|
14
13
|
|
|
15
14
|
it('creates the admin role and grants permissions to all actions', () => {
|
|
@@ -17,15 +16,14 @@ describe('API', () => {
|
|
|
17
16
|
for (const actionName of Object.keys(actions)) {
|
|
18
17
|
expect(role.allow).toHaveBeenCalledWith(actions[actionName], expect.any(Function));
|
|
19
18
|
}
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
})
|
|
19
|
+
});
|
|
20
|
+
});
|
|
23
21
|
|
|
24
22
|
describe('ensureActionExists', () => {
|
|
25
23
|
it('should throw a PermissionSystemError if the action is undefined', () => {
|
|
26
24
|
expect(() => ensureActionExists(undefined, {})).toThrow(PermissionSystemError);
|
|
27
25
|
});
|
|
28
|
-
|
|
26
|
+
|
|
29
27
|
it('should not throw an error if the action is defined', () => {
|
|
30
28
|
expect(() => ensureActionExists('some action', {})).not.toThrow();
|
|
31
29
|
});
|
|
@@ -38,40 +36,44 @@ describe('API', () => {
|
|
|
38
36
|
const key = 'some key';
|
|
39
37
|
expect(() => ensureIsFunction(null, action, options, key)).toThrow(PermissionSystemError);
|
|
40
38
|
});
|
|
41
|
-
|
|
39
|
+
|
|
42
40
|
it('should not throw an error if the provided value is a function', () => {
|
|
43
41
|
const action = 'some action';
|
|
44
42
|
const options = { showKey: true };
|
|
45
43
|
const key = 'some key';
|
|
46
|
-
expect(() =>
|
|
44
|
+
expect(() =>
|
|
45
|
+
ensureIsFunction(
|
|
46
|
+
() => {
|
|
47
|
+
/**/
|
|
48
|
+
},
|
|
49
|
+
action,
|
|
50
|
+
options,
|
|
51
|
+
key,
|
|
52
|
+
),
|
|
53
|
+
).not.toThrow();
|
|
47
54
|
});
|
|
48
55
|
});
|
|
49
56
|
|
|
50
|
-
|
|
51
57
|
describe('checkAction', () => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
it('should throw a NoPermissionError if the user does not have permission to perform the action', async () => {
|
|
59
|
+
Roles.userHasPermission = import.meta.jest.fn(async () => false);
|
|
60
|
+
|
|
61
|
+
const context = { userId: '123' };
|
|
62
|
+
const action = 'some action';
|
|
63
|
+
const args: any = [];
|
|
64
|
+
const options = { key: 'some key' };
|
|
55
65
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const args: any = [];
|
|
59
|
-
const options = { key: 'some key' };
|
|
66
|
+
return expect(checkAction(context, action, args, options)).rejects.toThrow(NoPermissionError);
|
|
67
|
+
});
|
|
60
68
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const action = 'some action';
|
|
68
|
-
const args: any = {};
|
|
69
|
-
const options = { key: 'some key' };
|
|
69
|
+
it('should not throw an error if the user has permission to perform the action', async () => {
|
|
70
|
+
Roles.userHasPermission = import.meta.jest.fn(async () => true);
|
|
71
|
+
const context = { userId: '123' };
|
|
72
|
+
const action = 'some action';
|
|
73
|
+
const args: any = {};
|
|
74
|
+
const options = { key: 'some key' };
|
|
70
75
|
|
|
71
|
-
|
|
76
|
+
return expect(checkAction(context, action, args, options)).resolves.not.toThrow();
|
|
77
|
+
});
|
|
72
78
|
});
|
|
73
79
|
});
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
});
|