@molecule/api-resource-user 1.6.0 → 1.7.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 (72) hide show
  1. package/README.md +226 -44
  2. package/dist/handlers/create.d.ts +19 -5
  3. package/dist/handlers/create.d.ts.map +1 -1
  4. package/dist/handlers/create.js +20 -5
  5. package/dist/handlers/create.js.map +1 -1
  6. package/dist/handlers/del.d.ts +2 -2
  7. package/dist/handlers/del.d.ts.map +1 -1
  8. package/dist/handlers/del.js +1 -1
  9. package/dist/handlers/del.js.map +1 -1
  10. package/dist/handlers/forgotPassword.d.ts +2 -2
  11. package/dist/handlers/forgotPassword.d.ts.map +1 -1
  12. package/dist/handlers/forgotPassword.js +2 -2
  13. package/dist/handlers/forgotPassword.js.map +1 -1
  14. package/dist/handlers/logIn.d.ts +13 -6
  15. package/dist/handlers/logIn.d.ts.map +1 -1
  16. package/dist/handlers/logIn.js +13 -6
  17. package/dist/handlers/logIn.js.map +1 -1
  18. package/dist/handlers/logInOAuth.d.ts +8 -4
  19. package/dist/handlers/logInOAuth.d.ts.map +1 -1
  20. package/dist/handlers/logInOAuth.js +9 -5
  21. package/dist/handlers/logInOAuth.js.map +1 -1
  22. package/dist/handlers/payments/billingPortal.d.ts +3 -0
  23. package/dist/handlers/payments/billingPortal.d.ts.map +1 -1
  24. package/dist/handlers/payments/billingPortal.js +10 -3
  25. package/dist/handlers/payments/billingPortal.js.map +1 -1
  26. package/dist/handlers/payments/handlePaymentNotification.d.ts +2 -2
  27. package/dist/handlers/payments/handlePaymentNotification.d.ts.map +1 -1
  28. package/dist/handlers/payments/handlePaymentNotification.js +2 -2
  29. package/dist/handlers/payments/handlePaymentNotification.js.map +1 -1
  30. package/dist/handlers/payments/verifyPayment.d.ts +5 -2
  31. package/dist/handlers/payments/verifyPayment.d.ts.map +1 -1
  32. package/dist/handlers/payments/verifyPayment.js +10 -3
  33. package/dist/handlers/payments/verifyPayment.js.map +1 -1
  34. package/dist/handlers/read.d.ts +2 -2
  35. package/dist/handlers/read.d.ts.map +1 -1
  36. package/dist/handlers/read.js +1 -1
  37. package/dist/handlers/read.js.map +1 -1
  38. package/dist/handlers/readSelf.d.ts +2 -2
  39. package/dist/handlers/readSelf.d.ts.map +1 -1
  40. package/dist/handlers/readSelf.js +1 -1
  41. package/dist/handlers/readSelf.js.map +1 -1
  42. package/dist/handlers/resetPassword.d.ts +2 -1
  43. package/dist/handlers/resetPassword.d.ts.map +1 -1
  44. package/dist/handlers/resetPassword.js +2 -1
  45. package/dist/handlers/resetPassword.js.map +1 -1
  46. package/dist/handlers/update.d.ts +19 -4
  47. package/dist/handlers/update.d.ts.map +1 -1
  48. package/dist/handlers/update.js +19 -4
  49. package/dist/handlers/update.js.map +1 -1
  50. package/dist/handlers/updatePassword.d.ts +12 -2
  51. package/dist/handlers/updatePassword.d.ts.map +1 -1
  52. package/dist/handlers/updatePassword.js +12 -2
  53. package/dist/handlers/updatePassword.js.map +1 -1
  54. package/dist/handlers/updatePlan.d.ts +6 -2
  55. package/dist/handlers/updatePlan.d.ts.map +1 -1
  56. package/dist/handlers/updatePlan.js +11 -3
  57. package/dist/handlers/updatePlan.js.map +1 -1
  58. package/dist/handlers/verifyTwoFactor.d.ts +2 -2
  59. package/dist/handlers/verifyTwoFactor.d.ts.map +1 -1
  60. package/dist/handlers/verifyTwoFactor.js +2 -2
  61. package/dist/handlers/verifyTwoFactor.js.map +1 -1
  62. package/dist/index.d.ts +86 -0
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +86 -0
  65. package/dist/index.js.map +1 -1
  66. package/dist/types.d.ts +38 -0
  67. package/dist/types.d.ts.map +1 -1
  68. package/dist/utilities/payments.d.ts +29 -0
  69. package/dist/utilities/payments.d.ts.map +1 -0
  70. package/dist/utilities/payments.js +47 -0
  71. package/dist/utilities/payments.js.map +1 -0
  72. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -63,6 +63,92 @@
63
63
  * Monkey-patching `res.json` to re-shape a handler's answer intercepts nothing the handler
64
64
  * produces, so the request it was meant to answer is left unanswered and hangs.
65
65
  *
66
+ * **Re-shaping a success answer (RealWorld's `{ user: { …, token } }`, say)? Read the token from
67
+ * `result.body.accessToken` — it is in the BODY.** The `Authorization` response header carries
68
+ * the same token prefixed with the mount's scheme (`Bearer <token>`), so it is not the raw value
69
+ * a client sends back. Type the result from the handler — do not cast `body` to `unknown`, which
70
+ * hides the success branch — and narrow on the body's own keys:
71
+ *
72
+ * ```typescript
73
+ * const result = await handlers.logIn(resource)(req, res)
74
+ * if (!('accessToken' in result.body)) {
75
+ * // 400/403/500 `{ error, errorKey }`, or 206 `{ twoFactorRequired: true }` — send as built.
76
+ * res.status(result.statusCode).send(result.body)
77
+ * return
78
+ * }
79
+ * // `props` and `user` are the SAFE columns (secret columns stripped); accessToken is the token.
80
+ * const { props, accessToken } = result.body
81
+ * res.status(200).json({ user: { ...props, token: accessToken } })
82
+ * ```
83
+ *
84
+ * **Constructing a handler: `schema` is optional.** Every factory accepts
85
+ * `{ name, tableName, schema? }`. `schema` is a Zod object (anything exposing `safeParse`)
86
+ * and only `handlers.create` / `handlers.logInOAuth` use it — both default to this package's
87
+ * `propsSchema`. `handlers.update` validates with its own update schema; login and the
88
+ * read/write handlers never validate props. The smallest correct wiring, with every import
89
+ * spelled out:
90
+ *
91
+ * ```typescript
92
+ * import { createRequestHandler } from '@molecule/api-resource'
93
+ * import { createResource, handlers } from '@molecule/api-resource-user'
94
+ *
95
+ * // Omit `schema`: create() validates new users with this package's propsSchema.
96
+ * const signUp = createRequestHandler(handlers.create({ name: 'User', tableName: 'users' }))
97
+ * const signIn = createRequestHandler(handlers.logIn({ name: 'User', tableName: 'users' }))
98
+ *
99
+ * // A constrained schema, when the app restricts oauthServer / planKey.
100
+ * const users = createResource({ oauthServers: ['google', 'github'] as const })
101
+ * const signUpConstrained = createRequestHandler(handlers.create(users))
102
+ * ```
103
+ *
104
+ * `schema: {}` (or any object without `safeParse`) is NOT a schema: the first request
105
+ * answers 400 `resource.error.unknownError` with
106
+ * `TypeError: schema.safeParse is not a function`, and no row is written. Omit `schema`
107
+ * instead of stubbing it.
108
+ *
109
+ * **Exact responses of the sign-up and sign-in handlers** (the body shapes the routes send;
110
+ * a handler returns `{ statusCode, body }` and the mount applies it):
111
+ * - `create` (`POST /api/users`, flat body): 201 `{ props, accessToken, user }` (`user` equals
112
+ * `props`, secret columns stripped); 400 `user.error.usernameInvalid` /
113
+ * `user.error.bodyNestedUnderUser` / `user.error.passwordRequired` /
114
+ * `user.error.passwordTooLong` / `user.error.passwordTooShort` / `user.error.emailInvalid` /
115
+ * `user.error.badRequest`, or `resource.error.unknownError` when props fail the schema;
116
+ * 403 `user.error.usernameUnavailable` / `user.error.emailAlreadyRegistered`;
117
+ * 500 `user.error.failedToHashPassword` / `user.error.failedToCreateSession`.
118
+ * - `logIn` (`POST /api/users/log-in`): 200 `{ props, accessToken, user }`; 206
119
+ * `{ twoFactorRequired: true }`; 403 `user.error.invalidCredentials` /
120
+ * `user.error.invalidTwoFactorToken`; 500 `user.error.failedToCreateSession` /
121
+ * `user.error.twoFactorVerificationUnavailable` / `user.error.loginFailed`. Bad credentials
122
+ * are 403, not 401 or 422.
123
+ *
124
+ * **A JWT you sign yourself is NOT a session.** `verifyMiddleware` accepts a token only when
125
+ * the bonded `@molecule/api-jwt` provider verifies it AND its `deviceId` names an ACTIVE row
126
+ * in `devices`; anything else — another secret or algorithm, or an invented/placeholder
127
+ * `deviceId` — is answered 401 on every request (a request with no credential at all answers
128
+ * `{"errors":{"token":["is missing"]}}`). Mint sessions with `authorization.set()` after
129
+ * `device.createOrUpdate()` (the sequence above) and mount `verifyMiddleware` once in front of
130
+ * the routes — never hand-roll auth middleware or a second token scheme.
131
+ *
132
+ * **Changing the EMAIL or the PASSWORD requires the CURRENT password — there is no opt-out.**
133
+ * `handlers.update` (`PATCH /users/:id`) takes profile fields; when the body changes (or
134
+ * clears) `email` on an account that has a password, the SAME flat body must also carry that
135
+ * account's `currentPassword`. Missing → 400 `user.error.currentPasswordRequired`; wrong → 403
136
+ * `user.error.currentPasswordIncorrect`. A real change resets `emailVerified` to `false`
137
+ * server-side (the new address is unproven), and `emailVerified` is NEVER read from the body.
138
+ * An OAuth-only account (no `passwordHash`) has no password to present, so its provider-backed
139
+ * session is the credential. `handlers.updatePassword` (`PATCH /users/:id/password`) requires
140
+ * `newPassword` (8–1024 characters) and, on a password account, `currentPassword` as well —
141
+ * same 400/403 codes; it answers 200 `{ success: true }`, clears any outstanding reset token,
142
+ * and revokes every OTHER device while the caller's own session keeps working.
143
+ *
144
+ * Do NOT work around either gate by storing, caching or replaying the user's password, by
145
+ * sending an invented `currentPassword`, or by writing `email` around the handler with the
146
+ * datastore: the password is the re-authentication factor that stops a hijacked session from
147
+ * moving the account-recovery address. If a wire format (e.g. RealWorld's token-only
148
+ * `PUT /user`) cannot collect the current password, that flow is simply unsupported as written
149
+ * — have the client ask for it in a re-authentication step, or route sign-in and account
150
+ * changes through the auth pages (`useAuth()`). There is no supported bypass to add.
151
+ *
66
152
  * @remarks
67
153
  * The user record is split across TWO schemas — pick the right one or you leak credentials:
68
154
  * - **{@link Props} (`propsSchema`)** — SAFE, client-facing fields (username, name, email,
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmMG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyRG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
package/dist/index.js CHANGED
@@ -63,6 +63,92 @@
63
63
  * Monkey-patching `res.json` to re-shape a handler's answer intercepts nothing the handler
64
64
  * produces, so the request it was meant to answer is left unanswered and hangs.
65
65
  *
66
+ * **Re-shaping a success answer (RealWorld's `{ user: { …, token } }`, say)? Read the token from
67
+ * `result.body.accessToken` — it is in the BODY.** The `Authorization` response header carries
68
+ * the same token prefixed with the mount's scheme (`Bearer <token>`), so it is not the raw value
69
+ * a client sends back. Type the result from the handler — do not cast `body` to `unknown`, which
70
+ * hides the success branch — and narrow on the body's own keys:
71
+ *
72
+ * ```typescript
73
+ * const result = await handlers.logIn(resource)(req, res)
74
+ * if (!('accessToken' in result.body)) {
75
+ * // 400/403/500 `{ error, errorKey }`, or 206 `{ twoFactorRequired: true }` — send as built.
76
+ * res.status(result.statusCode).send(result.body)
77
+ * return
78
+ * }
79
+ * // `props` and `user` are the SAFE columns (secret columns stripped); accessToken is the token.
80
+ * const { props, accessToken } = result.body
81
+ * res.status(200).json({ user: { ...props, token: accessToken } })
82
+ * ```
83
+ *
84
+ * **Constructing a handler: `schema` is optional.** Every factory accepts
85
+ * `{ name, tableName, schema? }`. `schema` is a Zod object (anything exposing `safeParse`)
86
+ * and only `handlers.create` / `handlers.logInOAuth` use it — both default to this package's
87
+ * `propsSchema`. `handlers.update` validates with its own update schema; login and the
88
+ * read/write handlers never validate props. The smallest correct wiring, with every import
89
+ * spelled out:
90
+ *
91
+ * ```typescript
92
+ * import { createRequestHandler } from '@molecule/api-resource'
93
+ * import { createResource, handlers } from '@molecule/api-resource-user'
94
+ *
95
+ * // Omit `schema`: create() validates new users with this package's propsSchema.
96
+ * const signUp = createRequestHandler(handlers.create({ name: 'User', tableName: 'users' }))
97
+ * const signIn = createRequestHandler(handlers.logIn({ name: 'User', tableName: 'users' }))
98
+ *
99
+ * // A constrained schema, when the app restricts oauthServer / planKey.
100
+ * const users = createResource({ oauthServers: ['google', 'github'] as const })
101
+ * const signUpConstrained = createRequestHandler(handlers.create(users))
102
+ * ```
103
+ *
104
+ * `schema: {}` (or any object without `safeParse`) is NOT a schema: the first request
105
+ * answers 400 `resource.error.unknownError` with
106
+ * `TypeError: schema.safeParse is not a function`, and no row is written. Omit `schema`
107
+ * instead of stubbing it.
108
+ *
109
+ * **Exact responses of the sign-up and sign-in handlers** (the body shapes the routes send;
110
+ * a handler returns `{ statusCode, body }` and the mount applies it):
111
+ * - `create` (`POST /api/users`, flat body): 201 `{ props, accessToken, user }` (`user` equals
112
+ * `props`, secret columns stripped); 400 `user.error.usernameInvalid` /
113
+ * `user.error.bodyNestedUnderUser` / `user.error.passwordRequired` /
114
+ * `user.error.passwordTooLong` / `user.error.passwordTooShort` / `user.error.emailInvalid` /
115
+ * `user.error.badRequest`, or `resource.error.unknownError` when props fail the schema;
116
+ * 403 `user.error.usernameUnavailable` / `user.error.emailAlreadyRegistered`;
117
+ * 500 `user.error.failedToHashPassword` / `user.error.failedToCreateSession`.
118
+ * - `logIn` (`POST /api/users/log-in`): 200 `{ props, accessToken, user }`; 206
119
+ * `{ twoFactorRequired: true }`; 403 `user.error.invalidCredentials` /
120
+ * `user.error.invalidTwoFactorToken`; 500 `user.error.failedToCreateSession` /
121
+ * `user.error.twoFactorVerificationUnavailable` / `user.error.loginFailed`. Bad credentials
122
+ * are 403, not 401 or 422.
123
+ *
124
+ * **A JWT you sign yourself is NOT a session.** `verifyMiddleware` accepts a token only when
125
+ * the bonded `@molecule/api-jwt` provider verifies it AND its `deviceId` names an ACTIVE row
126
+ * in `devices`; anything else — another secret or algorithm, or an invented/placeholder
127
+ * `deviceId` — is answered 401 on every request (a request with no credential at all answers
128
+ * `{"errors":{"token":["is missing"]}}`). Mint sessions with `authorization.set()` after
129
+ * `device.createOrUpdate()` (the sequence above) and mount `verifyMiddleware` once in front of
130
+ * the routes — never hand-roll auth middleware or a second token scheme.
131
+ *
132
+ * **Changing the EMAIL or the PASSWORD requires the CURRENT password — there is no opt-out.**
133
+ * `handlers.update` (`PATCH /users/:id`) takes profile fields; when the body changes (or
134
+ * clears) `email` on an account that has a password, the SAME flat body must also carry that
135
+ * account's `currentPassword`. Missing → 400 `user.error.currentPasswordRequired`; wrong → 403
136
+ * `user.error.currentPasswordIncorrect`. A real change resets `emailVerified` to `false`
137
+ * server-side (the new address is unproven), and `emailVerified` is NEVER read from the body.
138
+ * An OAuth-only account (no `passwordHash`) has no password to present, so its provider-backed
139
+ * session is the credential. `handlers.updatePassword` (`PATCH /users/:id/password`) requires
140
+ * `newPassword` (8–1024 characters) and, on a password account, `currentPassword` as well —
141
+ * same 400/403 codes; it answers 200 `{ success: true }`, clears any outstanding reset token,
142
+ * and revokes every OTHER device while the caller's own session keeps working.
143
+ *
144
+ * Do NOT work around either gate by storing, caching or replaying the user's password, by
145
+ * sending an invented `currentPassword`, or by writing `email` around the handler with the
146
+ * datastore: the password is the re-authentication factor that stops a hijacked session from
147
+ * moving the account-recovery address. If a wire format (e.g. RealWorld's token-only
148
+ * `PUT /user`) cannot collect the current password, that flow is simply unsupported as written
149
+ * — have the client ask for it in a re-authentication step, or route sign-in and account
150
+ * changes through the auth pages (`useAuth()`). There is no supported bypass to add.
151
+ *
66
152
  * @remarks
67
153
  * The user record is split across TWO schemas — pick the right one or you leak credentials:
68
154
  * - **{@link Props} (`propsSchema`)** — SAFE, client-facing fields (username, name, email,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmMG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyRG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
package/dist/types.d.ts CHANGED
@@ -11,4 +11,42 @@ export type { CreateOAuthProps, CreateProps, CreateSecretProps, Props, SecretPro
11
11
  * An object describing the `user` resource.
12
12
  */
13
13
  export type Resource<T = unknown> = resourceTypes.Resource<T>;
14
+ /**
15
+ * Configuration accepted by every user handler factory (`handlers.create`,
16
+ * `handlers.logIn`, `handlers.update`, …).
17
+ *
18
+ * `name` and `tableName` are the caller's to supply, because an app may mount the
19
+ * user resource on its own table. `schema` is OPTIONAL — no factory requires you
20
+ * to invent one:
21
+ *
22
+ * - `handlers.create` and `handlers.logInOAuth` validate the props they write and
23
+ * default to this package's {@link propsSchema} when `schema` is omitted.
24
+ * - `handlers.update` validates with the package's `updatePropsSchema` and ignores
25
+ * a supplied `schema`.
26
+ * - The remaining handlers read/write rows directly and never look at `schema`.
27
+ *
28
+ * Omission is NOT "no validation": the handlers that validate use
29
+ * {@link propsSchema}. Pass `createResource({ oauthServers, planKeys })` (or the
30
+ * `resource` export) when the app constrains `oauthServer` / `planKey`, and pass
31
+ * your own extended Zod object only when you add fields.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * handlers.create({ name: 'User', tableName: 'users' }) // validates with propsSchema
36
+ * handlers.create(createResource({ oauthServers: ['google'] as const }))
37
+ * ```
38
+ */
39
+ export interface HandlerResource {
40
+ /** The resource name used in error messages (e.g. `'User'`). */
41
+ name: string;
42
+ /** The database table name for users. */
43
+ tableName: string;
44
+ /**
45
+ * Optional Zod schema validated by `handlers.create` / `handlers.logInOAuth`.
46
+ * Defaults to {@link propsSchema}. Must expose `safeParse`; a stub object is not
47
+ * a schema and the first request answers 400 `resource.error.unknownError` with
48
+ * `TypeError: schema.safeParse is not a function`.
49
+ */
50
+ schema?: unknown;
51
+ }
14
52
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,KAAK,aAAa,MAAM,8BAA8B,CAAA;AAGlE,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,iBAAiB,EACjB,KAAK,EACL,WAAW,EACX,OAAO,EACP,yBAAyB,EACzB,eAAe,EACf,WAAW,EACX,oBAAoB,EACpB,0BAA0B,GAC3B,MAAM,aAAa,CAAA;AAEpB;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,GAAG,OAAO,IAAI,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,KAAK,aAAa,MAAM,8BAA8B,CAAA;AAGlE,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,iBAAiB,EACjB,KAAK,EACL,WAAW,EACX,OAAO,EACP,yBAAyB,EACzB,eAAe,EACf,WAAW,EACX,oBAAoB,EACpB,0BAA0B,GAC3B,MAAM,aAAa,CAAA;AAEpB;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,GAAG,OAAO,IAAI,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,WAAW,eAAe;IAC9B,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAA;IACZ,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAA;IACjB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Lazy access to `@molecule/api-payments`, which is an OPTIONAL peer of this package.
3
+ *
4
+ * The payment handlers need two runtime helpers from the core package
5
+ * (`isConfigNotConfiguredError` for the config-vs-failure split, and
6
+ * `resolveBillingPortalReturnUrl` for the portal exit origin), but the peer is declared
7
+ * `optional` in `peerDependenciesMeta` because an app that takes no payments must not be
8
+ * forced to install it. A static `import … from '@molecule/api-payments'` contradicts that:
9
+ * the package barrel exports every handler, so Node evaluates the import while loading
10
+ * `@molecule/api-resource-user` itself, and a production install (which installs no optional
11
+ * peer) dies at BOOT with `ERR_MODULE_NOT_FOUND … imported from
12
+ * …/dist/handlers/updatePlan.js` — xr-flash-m354, two failed deploys on 2026-10-21.
13
+ *
14
+ * A dynamic `import()` is evaluated only when a payments feature is actually used, and its
15
+ * absence means "this app has no payments", never a crash.
16
+ *
17
+ * @module
18
+ */
19
+ import type * as paymentsModule from '@molecule/api-payments';
20
+ /**
21
+ * Load `@molecule/api-payments` once, or `null` when the optional peer is not installed.
22
+ *
23
+ * Call it from a request path, never at module scope: the whole point is that importing
24
+ * this package does not require the peer.
25
+ *
26
+ * @returns The payments core module, or `null` when the app has not installed it.
27
+ */
28
+ export declare const loadPayments: () => Promise<typeof paymentsModule | null>;
29
+ //# sourceMappingURL=payments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payments.d.ts","sourceRoot":"","sources":["../../src/utilities/payments.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,KAAK,cAAc,MAAM,wBAAwB,CAAA;AAQ7D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,QAAO,OAAO,CAAC,OAAO,cAAc,GAAG,IAAI,CAgBnE,CAAA"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Lazy access to `@molecule/api-payments`, which is an OPTIONAL peer of this package.
3
+ *
4
+ * The payment handlers need two runtime helpers from the core package
5
+ * (`isConfigNotConfiguredError` for the config-vs-failure split, and
6
+ * `resolveBillingPortalReturnUrl` for the portal exit origin), but the peer is declared
7
+ * `optional` in `peerDependenciesMeta` because an app that takes no payments must not be
8
+ * forced to install it. A static `import … from '@molecule/api-payments'` contradicts that:
9
+ * the package barrel exports every handler, so Node evaluates the import while loading
10
+ * `@molecule/api-resource-user` itself, and a production install (which installs no optional
11
+ * peer) dies at BOOT with `ERR_MODULE_NOT_FOUND … imported from
12
+ * …/dist/handlers/updatePlan.js` — xr-flash-m354, two failed deploys on 2026-10-21.
13
+ *
14
+ * A dynamic `import()` is evaluated only when a payments feature is actually used, and its
15
+ * absence means "this app has no payments", never a crash.
16
+ *
17
+ * @module
18
+ */
19
+ /**
20
+ * The first load of the optional peer — the module, or `null` when it is not installed.
21
+ * Memoised so absence is probed once per process instead of on every request.
22
+ */
23
+ let paymentsModulePromise;
24
+ /**
25
+ * Load `@molecule/api-payments` once, or `null` when the optional peer is not installed.
26
+ *
27
+ * Call it from a request path, never at module scope: the whole point is that importing
28
+ * this package does not require the peer.
29
+ *
30
+ * @returns The payments core module, or `null` when the app has not installed it.
31
+ */
32
+ export const loadPayments = () => {
33
+ paymentsModulePromise ??= import('@molecule/api-payments').catch((error) => {
34
+ // Only absence of THIS optional peer is expected. A missing transitive import,
35
+ // invalid export or initialization failure in an installed package is a real
36
+ // failure and must reach the handler's normal error reporting.
37
+ const failure = error;
38
+ if (failure?.code === 'ERR_MODULE_NOT_FOUND' &&
39
+ typeof failure.message === 'string' &&
40
+ /^Cannot find package ['"]@molecule\/api-payments['"] imported from /.test(failure.message)) {
41
+ return null;
42
+ }
43
+ throw error;
44
+ });
45
+ return paymentsModulePromise;
46
+ };
47
+ //# sourceMappingURL=payments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payments.js","sourceRoot":"","sources":["../../src/utilities/payments.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH;;;GAGG;AACH,IAAI,qBAAwE,CAAA;AAE5E;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,GAA0C,EAAE;IACtE,qBAAqB,KAAK,MAAM,CAAC,wBAAwB,CAAC,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;QAClF,+EAA+E;QAC/E,6EAA6E;QAC7E,+DAA+D;QAC/D,MAAM,OAAO,GAAG,KAAqD,CAAA;QACrE,IACE,OAAO,EAAE,IAAI,KAAK,sBAAsB;YACxC,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ;YACnC,qEAAqE,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAC3F,CAAC;YACD,OAAO,IAAI,CAAA;QACb,CAAC;QACD,MAAM,KAAK,CAAA;IACb,CAAC,CAAC,CAAA;IACF,OAAO,qBAAqB,CAAA;AAC9B,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-resource-user",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "User resource types and schema for molecule.dev.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",