@unchainedshop/api 3.0.0-rc9 → 3.0.1
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/api-index.d.ts +3 -2
- package/lib/api-index.d.ts.map +1 -1
- package/lib/api-index.js +3 -2
- package/lib/api-index.js.map +1 -1
- package/lib/context.d.ts +17 -21
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +2 -2
- package/lib/context.js.map +1 -1
- package/lib/errors.d.ts +1 -0
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -0
- package/lib/errors.js.map +1 -1
- package/lib/express/createBulkImportMiddleware.d.ts.map +1 -1
- package/lib/express/createBulkImportMiddleware.js +3 -3
- package/lib/express/createBulkImportMiddleware.js.map +1 -1
- package/lib/express/createERCMetadataMiddleware.d.ts.map +1 -1
- package/lib/express/createERCMetadataMiddleware.js +9 -8
- package/lib/express/createERCMetadataMiddleware.js.map +1 -1
- package/lib/express/index.d.ts +2 -0
- package/lib/express/index.d.ts.map +1 -1
- package/lib/express/index.js +11 -2
- package/lib/express/index.js.map +1 -1
- package/lib/fastify/ercMetadataHandler.d.ts.map +1 -1
- package/lib/fastify/ercMetadataHandler.js +2 -1
- package/lib/fastify/ercMetadataHandler.js.map +1 -1
- package/lib/fastify/index.d.ts +4 -1
- package/lib/fastify/index.d.ts.map +1 -1
- package/lib/fastify/index.js +47 -15
- package/lib/fastify/index.js.map +1 -1
- package/lib/loaders/index.d.ts +103 -59
- package/lib/loaders/index.d.ts.map +1 -1
- package/lib/loaders/index.js +173 -111
- package/lib/loaders/index.js.map +1 -1
- package/lib/locale-context.d.ts +1 -6
- package/lib/locale-context.d.ts.map +1 -1
- package/lib/locale-context.js +2 -4
- package/lib/locale-context.js.map +1 -1
- package/lib/resolvers/mutations/accounts/createUser.js +1 -1
- package/lib/resolvers/mutations/accounts/createUser.js.map +1 -1
- package/lib/resolvers/mutations/accounts/impersonate.d.ts +9 -0
- package/lib/resolvers/mutations/accounts/impersonate.d.ts.map +1 -0
- package/lib/resolvers/mutations/accounts/impersonate.js +17 -0
- package/lib/resolvers/mutations/accounts/impersonate.js.map +1 -0
- package/lib/resolvers/mutations/accounts/loginAsGuest.js +1 -1
- package/lib/resolvers/mutations/accounts/loginAsGuest.js.map +1 -1
- package/lib/resolvers/mutations/accounts/loginWithPassword.js +1 -1
- package/lib/resolvers/mutations/accounts/loginWithPassword.js.map +1 -1
- package/lib/resolvers/mutations/accounts/loginWithWebAuthn.js +1 -1
- package/lib/resolvers/mutations/accounts/loginWithWebAuthn.js.map +1 -1
- package/lib/resolvers/mutations/accounts/resetPassword.js +1 -1
- package/lib/resolvers/mutations/accounts/resetPassword.js.map +1 -1
- package/lib/resolvers/mutations/accounts/stopImpersonation.d.ts +6 -0
- package/lib/resolvers/mutations/accounts/stopImpersonation.d.ts.map +1 -0
- package/lib/resolvers/mutations/accounts/stopImpersonation.js +14 -0
- package/lib/resolvers/mutations/accounts/stopImpersonation.js.map +1 -0
- package/lib/resolvers/mutations/accounts/verifyEmail.js +1 -1
- package/lib/resolvers/mutations/accounts/verifyEmail.js.map +1 -1
- package/lib/resolvers/mutations/assortments/removeAssortmentProduct.js +1 -1
- package/lib/resolvers/mutations/assortments/removeAssortmentProduct.js.map +1 -1
- package/lib/resolvers/mutations/index.d.ts +2 -0
- package/lib/resolvers/mutations/index.d.ts.map +1 -1
- package/lib/resolvers/mutations/index.js +5 -1
- package/lib/resolvers/mutations/index.js.map +1 -1
- package/lib/resolvers/mutations/products/removeProductReviewVote.d.ts +1 -1
- package/lib/resolvers/mutations/products/removeProductReviewVote.d.ts.map +1 -1
- package/lib/resolvers/mutations/users/addPushSubscription.d.ts.map +1 -1
- package/lib/resolvers/mutations/users/addPushSubscription.js +2 -1
- package/lib/resolvers/mutations/users/addPushSubscription.js.map +1 -1
- package/lib/resolvers/mutations/users/heartbeat.d.ts +1 -1
- package/lib/resolvers/mutations/users/heartbeat.d.ts.map +1 -1
- package/lib/resolvers/mutations/users/heartbeat.js +2 -2
- package/lib/resolvers/mutations/users/heartbeat.js.map +1 -1
- package/lib/resolvers/mutations/users/removePushSubscription.d.ts.map +1 -1
- package/lib/resolvers/mutations/users/removePushSubscription.js +2 -1
- package/lib/resolvers/mutations/users/removePushSubscription.js.map +1 -1
- package/lib/resolvers/queries/index.d.ts +3 -0
- package/lib/resolvers/queries/index.d.ts.map +1 -1
- package/lib/resolvers/queries/index.js +4 -0
- package/lib/resolvers/queries/index.js.map +1 -1
- package/lib/resolvers/queries/shopInfo.d.ts +0 -1
- package/lib/resolvers/queries/shopInfo.d.ts.map +1 -1
- package/lib/resolvers/queries/shopInfo.js +10 -9
- package/lib/resolvers/queries/shopInfo.js.map +1 -1
- package/lib/resolvers/queries/users/impersonator.d.ts +3 -0
- package/lib/resolvers/queries/users/impersonator.d.ts.map +1 -0
- package/lib/resolvers/queries/users/impersonator.js +8 -0
- package/lib/resolvers/queries/users/impersonator.js.map +1 -0
- package/lib/resolvers/queries/warehousing/tokens.d.ts +2 -1
- package/lib/resolvers/queries/warehousing/tokens.d.ts.map +1 -1
- package/lib/resolvers/queries/warehousing/tokens.js +2 -2
- package/lib/resolvers/queries/warehousing/tokens.js.map +1 -1
- package/lib/resolvers/queries/warehousing/tokensCount.d.ts +5 -0
- package/lib/resolvers/queries/warehousing/tokensCount.d.ts.map +1 -0
- package/lib/resolvers/queries/warehousing/tokensCount.js +6 -0
- package/lib/resolvers/queries/warehousing/tokensCount.js.map +1 -0
- package/lib/resolvers/type/assortment/assortment-path-link-types.d.ts +7 -9
- package/lib/resolvers/type/assortment/assortment-path-link-types.d.ts.map +1 -1
- package/lib/resolvers/type/assortment/assortment-path-link-types.js +9 -7
- package/lib/resolvers/type/assortment/assortment-path-link-types.js.map +1 -1
- package/lib/resolvers/type/assortment/assortment-types.d.ts +1 -1
- package/lib/resolvers/type/assortment/assortment-types.d.ts.map +1 -1
- package/lib/resolvers/type/assortment/assortment-types.js +9 -2
- package/lib/resolvers/type/assortment/assortment-types.js.map +1 -1
- package/lib/resolvers/type/index.d.ts +14 -8
- package/lib/resolvers/type/index.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-bundle-types.d.ts +1 -1
- package/lib/resolvers/type/product/product-bundle-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-configurable-types.d.ts +1 -1
- package/lib/resolvers/type/product/product-configurable-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-plan-types.d.ts +1 -1
- package/lib/resolvers/type/product/product-plan-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-simple-types.d.ts +1 -1
- package/lib/resolvers/type/product/product-simple-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-tokenized-types.d.ts +1 -1
- package/lib/resolvers/type/product/product-tokenized-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-types.d.ts +1 -1
- package/lib/resolvers/type/product/product-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-types.js +9 -2
- package/lib/resolvers/type/product/product-types.js.map +1 -1
- package/lib/resolvers/type/product/product-variation-assignment-vector.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-variation-assignment-vector.js +4 -1
- package/lib/resolvers/type/product/product-variation-assignment-vector.js.map +1 -1
- package/lib/resolvers/type/product/product-variation-types.d.ts.map +1 -1
- package/lib/resolvers/type/product/product-variation-types.js +5 -2
- package/lib/resolvers/type/product/product-variation-types.js.map +1 -1
- package/lib/roles/all.d.ts.map +1 -1
- package/lib/roles/all.js +2 -0
- package/lib/roles/all.js.map +1 -1
- package/lib/roles/index.d.ts.map +1 -1
- package/lib/roles/index.js +2 -0
- package/lib/roles/index.js.map +1 -1
- package/lib/schema/mutation.d.ts.map +1 -1
- package/lib/schema/mutation.js +10 -0
- package/lib/schema/mutation.js.map +1 -1
- package/lib/schema/query.d.ts.map +1 -1
- package/lib/schema/query.js +11 -1
- package/lib/schema/query.js.map +1 -1
- package/lib/schema/types/shop.d.ts.map +1 -1
- package/lib/schema/types/shop.js +3 -2
- package/lib/schema/types/shop.js.map +1 -1
- package/lib/schema/types/user.js +1 -1
- package/package.json +20 -20
- package/src/api-index.ts +5 -4
- package/src/context.ts +32 -17
- package/src/errors.ts +5 -0
- package/src/express/createBulkImportMiddleware.ts +3 -3
- package/src/express/createERCMetadataMiddleware.ts +10 -8
- package/src/express/index.ts +29 -13
- package/src/fastify/ercMetadataHandler.ts +3 -1
- package/src/fastify/index.ts +67 -23
- package/src/loaders/index.ts +273 -228
- package/src/locale-context.ts +1 -10
- package/src/resolvers/mutations/accounts/createUser.ts +1 -1
- package/src/resolvers/mutations/accounts/impersonate.ts +23 -0
- package/src/resolvers/mutations/accounts/loginAsGuest.ts +1 -1
- package/src/resolvers/mutations/accounts/loginWithPassword.ts +1 -1
- package/src/resolvers/mutations/accounts/loginWithWebAuthn.ts +1 -1
- package/src/resolvers/mutations/accounts/resetPassword.ts +1 -1
- package/src/resolvers/mutations/accounts/stopImpersonation.ts +19 -0
- package/src/resolvers/mutations/accounts/verifyEmail.ts +1 -1
- package/src/resolvers/mutations/assortments/removeAssortmentProduct.ts +1 -1
- package/src/resolvers/mutations/index.ts +6 -1
- package/src/resolvers/mutations/users/addPushSubscription.ts +2 -1
- package/src/resolvers/mutations/users/heartbeat.ts +2 -2
- package/src/resolvers/mutations/users/removePushSubscription.ts +3 -1
- package/src/resolvers/queries/index.ts +4 -0
- package/src/resolvers/queries/shopInfo.ts +9 -9
- package/src/resolvers/queries/users/impersonator.ts +13 -0
- package/src/resolvers/queries/warehousing/tokens.ts +3 -1
- package/src/resolvers/queries/warehousing/tokensCount.ts +12 -0
- package/src/resolvers/type/assortment/assortment-path-link-types.ts +14 -19
- package/src/resolvers/type/assortment/assortment-types.ts +17 -5
- package/src/resolvers/type/product/product-types.ts +17 -5
- package/src/resolvers/type/product/product-variation-assignment-vector.ts +4 -1
- package/src/resolvers/type/product/product-variation-types.ts +5 -2
- package/src/roles/all.ts +2 -0
- package/src/roles/index.ts +2 -0
- package/src/schema/mutation.ts +10 -0
- package/src/schema/query.ts +11 -1
- package/src/schema/types/shop.ts +3 -2
- package/src/schema/types/user.ts +1 -1
package/src/fastify/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getCurrentContextResolver } from '../context.js';
|
|
1
|
+
import { getCurrentContextResolver, LoginFn, LogoutFn } from '../context.js';
|
|
2
2
|
import bulkImportHandler from './bulkImportHandler.js';
|
|
3
3
|
import ercMetadataHandler from './ercMetadataHandler.js';
|
|
4
4
|
import MongoStore from 'connect-mongo';
|
|
@@ -10,7 +10,8 @@ import { API_EVENTS } from '../events.js';
|
|
|
10
10
|
import { User } from '@unchainedshop/core-users';
|
|
11
11
|
import fastifySession from '@fastify/session';
|
|
12
12
|
import fastifyCookie from '@fastify/cookie';
|
|
13
|
-
import { FastifyInstance, FastifyRequest } from 'fastify';
|
|
13
|
+
import { FastifyBaseLogger, FastifyInstance, FastifyRequest } from 'fastify';
|
|
14
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
14
15
|
|
|
15
16
|
const resolveUserRemoteAddress = (req: FastifyRequest) => {
|
|
16
17
|
const remoteAddress =
|
|
@@ -41,44 +42,69 @@ const middlewareHook = async function middlewareHook(req: any, reply: any) {
|
|
|
41
42
|
|
|
42
43
|
const context = getCurrentContextResolver();
|
|
43
44
|
|
|
44
|
-
async function
|
|
45
|
-
|
|
45
|
+
const login: LoginFn = async function (user: User, options = {}) {
|
|
46
|
+
const { impersonator } = options;
|
|
47
|
+
|
|
48
|
+
req.session.userId = user._id;
|
|
49
|
+
req.session.impersonatorId = impersonator?._id;
|
|
50
|
+
|
|
46
51
|
const tokenObject = {
|
|
47
52
|
_id: req.session.sessionId,
|
|
48
53
|
userId: user._id,
|
|
49
|
-
|
|
50
|
-
tokenExpires: new Date((req as any).session
|
|
54
|
+
// eslint-disable-next-line
|
|
55
|
+
tokenExpires: new Date((req as any).session.cookie._expires),
|
|
51
56
|
};
|
|
52
57
|
await emit(API_EVENTS.API_LOGIN_TOKEN_CREATED, tokenObject);
|
|
53
58
|
/* eslint-disable-next-line */
|
|
54
59
|
(user as any)._inLoginMethodResponse = true;
|
|
55
60
|
return { user, ...tokenObject };
|
|
56
|
-
}
|
|
61
|
+
};
|
|
57
62
|
|
|
58
|
-
async function logout() {
|
|
63
|
+
const logout: LogoutFn = async function logout() {
|
|
59
64
|
/* eslint-disable-line */
|
|
60
|
-
if (!req.session?.user?._id) return false;
|
|
61
65
|
const tokenObject = {
|
|
62
66
|
_id: (req as any).session.sessionId,
|
|
63
|
-
userId: req.session?.
|
|
67
|
+
userId: req.session?.userId,
|
|
64
68
|
};
|
|
65
|
-
req.session.
|
|
69
|
+
req.session.userId = null;
|
|
66
70
|
await emit(API_EVENTS.API_LOGOUT, tokenObject);
|
|
67
71
|
return true;
|
|
68
|
-
}
|
|
72
|
+
};
|
|
69
73
|
|
|
70
74
|
const [, accessToken] = req.headers.authorization?.split(' ') || [];
|
|
71
75
|
|
|
72
|
-
(req as any).unchainedContext = await context(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
(req as any).unchainedContext = await context(
|
|
77
|
+
{
|
|
78
|
+
setHeader,
|
|
79
|
+
getHeader,
|
|
80
|
+
remoteAddress,
|
|
81
|
+
remotePort,
|
|
82
|
+
login,
|
|
83
|
+
logout,
|
|
84
|
+
accessToken,
|
|
85
|
+
userId: req.session.userId,
|
|
86
|
+
impersonatorId: req.session.impersonatorId,
|
|
87
|
+
},
|
|
88
|
+
req,
|
|
89
|
+
reply,
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const unchainedLogger = (prefix: string): FastifyBaseLogger => {
|
|
94
|
+
const logger = createLogger(prefix);
|
|
95
|
+
function Logger(...args) {
|
|
96
|
+
this.args = args;
|
|
97
|
+
}
|
|
98
|
+
Logger.prototype.info = logger.info;
|
|
99
|
+
Logger.prototype.error = logger.error;
|
|
100
|
+
Logger.prototype.debug = logger.debug;
|
|
101
|
+
Logger.prototype.fatal = logger.error;
|
|
102
|
+
Logger.prototype.warn = logger.warn;
|
|
103
|
+
Logger.prototype.trace = logger.trace;
|
|
104
|
+
Logger.prototype.child = function () {
|
|
105
|
+
return new Logger();
|
|
106
|
+
};
|
|
107
|
+
return new Logger();
|
|
82
108
|
};
|
|
83
109
|
|
|
84
110
|
export const connect = (
|
|
@@ -87,7 +113,23 @@ export const connect = (
|
|
|
87
113
|
graphqlHandler,
|
|
88
114
|
db,
|
|
89
115
|
}: { graphqlHandler: YogaServerInstance<any, any>; db: mongodb.Db; unchainedAPI: UnchainedCore },
|
|
116
|
+
{
|
|
117
|
+
allowRemoteToLocalhostSecureCookies = false,
|
|
118
|
+
}: { allowRemoteToLocalhostSecureCookies?: boolean } = {},
|
|
90
119
|
) => {
|
|
120
|
+
if (allowRemoteToLocalhostSecureCookies) {
|
|
121
|
+
// Workaround: Allow to use sandbox with localhost
|
|
122
|
+
// https://github.com/fastify/fastify-cookie/issues/308
|
|
123
|
+
fastify.addHook('preHandler', async function (request) {
|
|
124
|
+
request.headers['x-forwarded-proto'] = 'https';
|
|
125
|
+
});
|
|
126
|
+
fastify.addHook('onSend', async function (_, reply) {
|
|
127
|
+
reply.headers({
|
|
128
|
+
'Access-Control-Allow-Private-Network': 'true',
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
91
133
|
const cookieName = UNCHAINED_COOKIE_NAME;
|
|
92
134
|
const domain = UNCHAINED_COOKIE_DOMAIN;
|
|
93
135
|
const path = UNCHAINED_COOKIE_PATH;
|
|
@@ -100,7 +142,9 @@ export const connect = (
|
|
|
100
142
|
'0': false,
|
|
101
143
|
}[UNCHAINED_COOKIE_SAMESITE?.trim()?.toLowerCase()] || false) as boolean | 'none' | 'lax' | 'strict';
|
|
102
144
|
|
|
103
|
-
fastify.
|
|
145
|
+
if (!fastify.hasPlugin('@fastify/cookie')) {
|
|
146
|
+
fastify.register(fastifyCookie);
|
|
147
|
+
}
|
|
104
148
|
fastify.register(fastifySession, {
|
|
105
149
|
secret: process.env.UNCHAINED_TOKEN_SECRET,
|
|
106
150
|
cookieName,
|