@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.
Files changed (181) hide show
  1. package/lib/api-index.d.ts +3 -2
  2. package/lib/api-index.d.ts.map +1 -1
  3. package/lib/api-index.js +3 -2
  4. package/lib/api-index.js.map +1 -1
  5. package/lib/context.d.ts +17 -21
  6. package/lib/context.d.ts.map +1 -1
  7. package/lib/context.js +2 -2
  8. package/lib/context.js.map +1 -1
  9. package/lib/errors.d.ts +1 -0
  10. package/lib/errors.d.ts.map +1 -1
  11. package/lib/errors.js +1 -0
  12. package/lib/errors.js.map +1 -1
  13. package/lib/express/createBulkImportMiddleware.d.ts.map +1 -1
  14. package/lib/express/createBulkImportMiddleware.js +3 -3
  15. package/lib/express/createBulkImportMiddleware.js.map +1 -1
  16. package/lib/express/createERCMetadataMiddleware.d.ts.map +1 -1
  17. package/lib/express/createERCMetadataMiddleware.js +9 -8
  18. package/lib/express/createERCMetadataMiddleware.js.map +1 -1
  19. package/lib/express/index.d.ts +2 -0
  20. package/lib/express/index.d.ts.map +1 -1
  21. package/lib/express/index.js +11 -2
  22. package/lib/express/index.js.map +1 -1
  23. package/lib/fastify/ercMetadataHandler.d.ts.map +1 -1
  24. package/lib/fastify/ercMetadataHandler.js +2 -1
  25. package/lib/fastify/ercMetadataHandler.js.map +1 -1
  26. package/lib/fastify/index.d.ts +4 -1
  27. package/lib/fastify/index.d.ts.map +1 -1
  28. package/lib/fastify/index.js +47 -15
  29. package/lib/fastify/index.js.map +1 -1
  30. package/lib/loaders/index.d.ts +103 -59
  31. package/lib/loaders/index.d.ts.map +1 -1
  32. package/lib/loaders/index.js +173 -111
  33. package/lib/loaders/index.js.map +1 -1
  34. package/lib/locale-context.d.ts +1 -6
  35. package/lib/locale-context.d.ts.map +1 -1
  36. package/lib/locale-context.js +2 -4
  37. package/lib/locale-context.js.map +1 -1
  38. package/lib/resolvers/mutations/accounts/createUser.js +1 -1
  39. package/lib/resolvers/mutations/accounts/createUser.js.map +1 -1
  40. package/lib/resolvers/mutations/accounts/impersonate.d.ts +9 -0
  41. package/lib/resolvers/mutations/accounts/impersonate.d.ts.map +1 -0
  42. package/lib/resolvers/mutations/accounts/impersonate.js +17 -0
  43. package/lib/resolvers/mutations/accounts/impersonate.js.map +1 -0
  44. package/lib/resolvers/mutations/accounts/loginAsGuest.js +1 -1
  45. package/lib/resolvers/mutations/accounts/loginAsGuest.js.map +1 -1
  46. package/lib/resolvers/mutations/accounts/loginWithPassword.js +1 -1
  47. package/lib/resolvers/mutations/accounts/loginWithPassword.js.map +1 -1
  48. package/lib/resolvers/mutations/accounts/loginWithWebAuthn.js +1 -1
  49. package/lib/resolvers/mutations/accounts/loginWithWebAuthn.js.map +1 -1
  50. package/lib/resolvers/mutations/accounts/resetPassword.js +1 -1
  51. package/lib/resolvers/mutations/accounts/resetPassword.js.map +1 -1
  52. package/lib/resolvers/mutations/accounts/stopImpersonation.d.ts +6 -0
  53. package/lib/resolvers/mutations/accounts/stopImpersonation.d.ts.map +1 -0
  54. package/lib/resolvers/mutations/accounts/stopImpersonation.js +14 -0
  55. package/lib/resolvers/mutations/accounts/stopImpersonation.js.map +1 -0
  56. package/lib/resolvers/mutations/accounts/verifyEmail.js +1 -1
  57. package/lib/resolvers/mutations/accounts/verifyEmail.js.map +1 -1
  58. package/lib/resolvers/mutations/assortments/removeAssortmentProduct.js +1 -1
  59. package/lib/resolvers/mutations/assortments/removeAssortmentProduct.js.map +1 -1
  60. package/lib/resolvers/mutations/index.d.ts +2 -0
  61. package/lib/resolvers/mutations/index.d.ts.map +1 -1
  62. package/lib/resolvers/mutations/index.js +5 -1
  63. package/lib/resolvers/mutations/index.js.map +1 -1
  64. package/lib/resolvers/mutations/products/removeProductReviewVote.d.ts +1 -1
  65. package/lib/resolvers/mutations/products/removeProductReviewVote.d.ts.map +1 -1
  66. package/lib/resolvers/mutations/users/addPushSubscription.d.ts.map +1 -1
  67. package/lib/resolvers/mutations/users/addPushSubscription.js +2 -1
  68. package/lib/resolvers/mutations/users/addPushSubscription.js.map +1 -1
  69. package/lib/resolvers/mutations/users/heartbeat.d.ts +1 -1
  70. package/lib/resolvers/mutations/users/heartbeat.d.ts.map +1 -1
  71. package/lib/resolvers/mutations/users/heartbeat.js +2 -2
  72. package/lib/resolvers/mutations/users/heartbeat.js.map +1 -1
  73. package/lib/resolvers/mutations/users/removePushSubscription.d.ts.map +1 -1
  74. package/lib/resolvers/mutations/users/removePushSubscription.js +2 -1
  75. package/lib/resolvers/mutations/users/removePushSubscription.js.map +1 -1
  76. package/lib/resolvers/queries/index.d.ts +3 -0
  77. package/lib/resolvers/queries/index.d.ts.map +1 -1
  78. package/lib/resolvers/queries/index.js +4 -0
  79. package/lib/resolvers/queries/index.js.map +1 -1
  80. package/lib/resolvers/queries/shopInfo.d.ts +0 -1
  81. package/lib/resolvers/queries/shopInfo.d.ts.map +1 -1
  82. package/lib/resolvers/queries/shopInfo.js +10 -9
  83. package/lib/resolvers/queries/shopInfo.js.map +1 -1
  84. package/lib/resolvers/queries/users/impersonator.d.ts +3 -0
  85. package/lib/resolvers/queries/users/impersonator.d.ts.map +1 -0
  86. package/lib/resolvers/queries/users/impersonator.js +8 -0
  87. package/lib/resolvers/queries/users/impersonator.js.map +1 -0
  88. package/lib/resolvers/queries/warehousing/tokens.d.ts +2 -1
  89. package/lib/resolvers/queries/warehousing/tokens.d.ts.map +1 -1
  90. package/lib/resolvers/queries/warehousing/tokens.js +2 -2
  91. package/lib/resolvers/queries/warehousing/tokens.js.map +1 -1
  92. package/lib/resolvers/queries/warehousing/tokensCount.d.ts +5 -0
  93. package/lib/resolvers/queries/warehousing/tokensCount.d.ts.map +1 -0
  94. package/lib/resolvers/queries/warehousing/tokensCount.js +6 -0
  95. package/lib/resolvers/queries/warehousing/tokensCount.js.map +1 -0
  96. package/lib/resolvers/type/assortment/assortment-path-link-types.d.ts +7 -9
  97. package/lib/resolvers/type/assortment/assortment-path-link-types.d.ts.map +1 -1
  98. package/lib/resolvers/type/assortment/assortment-path-link-types.js +9 -7
  99. package/lib/resolvers/type/assortment/assortment-path-link-types.js.map +1 -1
  100. package/lib/resolvers/type/assortment/assortment-types.d.ts +1 -1
  101. package/lib/resolvers/type/assortment/assortment-types.d.ts.map +1 -1
  102. package/lib/resolvers/type/assortment/assortment-types.js +9 -2
  103. package/lib/resolvers/type/assortment/assortment-types.js.map +1 -1
  104. package/lib/resolvers/type/index.d.ts +14 -8
  105. package/lib/resolvers/type/index.d.ts.map +1 -1
  106. package/lib/resolvers/type/product/product-bundle-types.d.ts +1 -1
  107. package/lib/resolvers/type/product/product-bundle-types.d.ts.map +1 -1
  108. package/lib/resolvers/type/product/product-configurable-types.d.ts +1 -1
  109. package/lib/resolvers/type/product/product-configurable-types.d.ts.map +1 -1
  110. package/lib/resolvers/type/product/product-plan-types.d.ts +1 -1
  111. package/lib/resolvers/type/product/product-plan-types.d.ts.map +1 -1
  112. package/lib/resolvers/type/product/product-simple-types.d.ts +1 -1
  113. package/lib/resolvers/type/product/product-simple-types.d.ts.map +1 -1
  114. package/lib/resolvers/type/product/product-tokenized-types.d.ts +1 -1
  115. package/lib/resolvers/type/product/product-tokenized-types.d.ts.map +1 -1
  116. package/lib/resolvers/type/product/product-types.d.ts +1 -1
  117. package/lib/resolvers/type/product/product-types.d.ts.map +1 -1
  118. package/lib/resolvers/type/product/product-types.js +9 -2
  119. package/lib/resolvers/type/product/product-types.js.map +1 -1
  120. package/lib/resolvers/type/product/product-variation-assignment-vector.d.ts.map +1 -1
  121. package/lib/resolvers/type/product/product-variation-assignment-vector.js +4 -1
  122. package/lib/resolvers/type/product/product-variation-assignment-vector.js.map +1 -1
  123. package/lib/resolvers/type/product/product-variation-types.d.ts.map +1 -1
  124. package/lib/resolvers/type/product/product-variation-types.js +5 -2
  125. package/lib/resolvers/type/product/product-variation-types.js.map +1 -1
  126. package/lib/roles/all.d.ts.map +1 -1
  127. package/lib/roles/all.js +2 -0
  128. package/lib/roles/all.js.map +1 -1
  129. package/lib/roles/index.d.ts.map +1 -1
  130. package/lib/roles/index.js +2 -0
  131. package/lib/roles/index.js.map +1 -1
  132. package/lib/schema/mutation.d.ts.map +1 -1
  133. package/lib/schema/mutation.js +10 -0
  134. package/lib/schema/mutation.js.map +1 -1
  135. package/lib/schema/query.d.ts.map +1 -1
  136. package/lib/schema/query.js +11 -1
  137. package/lib/schema/query.js.map +1 -1
  138. package/lib/schema/types/shop.d.ts.map +1 -1
  139. package/lib/schema/types/shop.js +3 -2
  140. package/lib/schema/types/shop.js.map +1 -1
  141. package/lib/schema/types/user.js +1 -1
  142. package/package.json +20 -20
  143. package/src/api-index.ts +5 -4
  144. package/src/context.ts +32 -17
  145. package/src/errors.ts +5 -0
  146. package/src/express/createBulkImportMiddleware.ts +3 -3
  147. package/src/express/createERCMetadataMiddleware.ts +10 -8
  148. package/src/express/index.ts +29 -13
  149. package/src/fastify/ercMetadataHandler.ts +3 -1
  150. package/src/fastify/index.ts +67 -23
  151. package/src/loaders/index.ts +273 -228
  152. package/src/locale-context.ts +1 -10
  153. package/src/resolvers/mutations/accounts/createUser.ts +1 -1
  154. package/src/resolvers/mutations/accounts/impersonate.ts +23 -0
  155. package/src/resolvers/mutations/accounts/loginAsGuest.ts +1 -1
  156. package/src/resolvers/mutations/accounts/loginWithPassword.ts +1 -1
  157. package/src/resolvers/mutations/accounts/loginWithWebAuthn.ts +1 -1
  158. package/src/resolvers/mutations/accounts/resetPassword.ts +1 -1
  159. package/src/resolvers/mutations/accounts/stopImpersonation.ts +19 -0
  160. package/src/resolvers/mutations/accounts/verifyEmail.ts +1 -1
  161. package/src/resolvers/mutations/assortments/removeAssortmentProduct.ts +1 -1
  162. package/src/resolvers/mutations/index.ts +6 -1
  163. package/src/resolvers/mutations/users/addPushSubscription.ts +2 -1
  164. package/src/resolvers/mutations/users/heartbeat.ts +2 -2
  165. package/src/resolvers/mutations/users/removePushSubscription.ts +3 -1
  166. package/src/resolvers/queries/index.ts +4 -0
  167. package/src/resolvers/queries/shopInfo.ts +9 -9
  168. package/src/resolvers/queries/users/impersonator.ts +13 -0
  169. package/src/resolvers/queries/warehousing/tokens.ts +3 -1
  170. package/src/resolvers/queries/warehousing/tokensCount.ts +12 -0
  171. package/src/resolvers/type/assortment/assortment-path-link-types.ts +14 -19
  172. package/src/resolvers/type/assortment/assortment-types.ts +17 -5
  173. package/src/resolvers/type/product/product-types.ts +17 -5
  174. package/src/resolvers/type/product/product-variation-assignment-vector.ts +4 -1
  175. package/src/resolvers/type/product/product-variation-types.ts +5 -2
  176. package/src/roles/all.ts +2 -0
  177. package/src/roles/index.ts +2 -0
  178. package/src/schema/mutation.ts +10 -0
  179. package/src/schema/query.ts +11 -1
  180. package/src/schema/types/shop.ts +3 -2
  181. package/src/schema/types/user.ts +1 -1
@@ -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 login(user: User) {
45
- req.session.user = user;
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
- /* eslint-disable-next-line */
50
- tokenExpires: new Date((req as any).session?.cookie._expires),
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?.user?._id,
67
+ userId: req.session?.userId,
64
68
  };
65
- req.session.user = null;
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
- setHeader,
74
- getHeader,
75
- remoteAddress,
76
- remotePort,
77
- login,
78
- logout,
79
- accessToken,
80
- userId: req.session.user?._id,
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.register(fastifyCookie);
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,