nitro-web 0.3.26 → 0.3.28

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/client/app.tsx CHANGED
@@ -1,3 +1,5 @@
1
+ // Pulls in axios-retry's types for the 'axios-retry' config key below
2
+ /// <reference types="axios-retry" />
1
3
  import { createBrowserRouter, createHashRouter, redirect, RouterProvider } from 'react-router-dom'
2
4
  import { Fragment, ReactNode } from 'react'
3
5
  import ReactDOM from 'react-dom/client'
@@ -39,7 +39,7 @@ export function useFetchDoc<T>(endpoint?: string, queryString?: string) {
39
39
 
40
40
  /** Fetches a collection */
41
41
  export function useFetchCol<T>(endpoint?: string, queryString?: string) {
42
- return useFetchResource<T>(endpoint, queryString) as CollectionReturn<T>
42
+ return useFetchResource<T>(endpoint, queryString || '') as CollectionReturn<T>
43
43
  }
44
44
 
45
45
  /** Fetches a shared collection (e.g. options), which is cached on the store */
@@ -12,6 +12,8 @@ import { isArray, pick, ucFirst, fullNameSplit, isEmail } from 'nitro-web/util'
12
12
  // Todo: detect if the user is already invited to the company, instead of token error
13
13
 
14
14
  const JWT_SECRET = process.env.JWT_SECRET || 'replace_this_with_secure_env_secret'
15
+ const resetMaxAge = 1000 * 60 * 60 * 48 // 48 hours
16
+ const inviteMaxAge = 1000 * 60 * 60 * 24 * 182 // 6 months
15
17
  let authConfig = null
16
18
  const resolveBaseUrlWarned = new Set()
17
19
  const requiredConfigKeys = [...requiredEmailConfigKeys]
@@ -31,7 +33,7 @@ export const routes = {
31
33
  'post /api/signin': [signin], // [todo: route gaurd basic limiter]
32
34
  'post /api/signup': [signup], // [todo: route gaurd Altcha]
33
35
  'post /api/reset-instructions': [resetInstructions],
34
- 'post /api/reset-confirm': [resetConfirm], // was reset-password
36
+ 'post /api/reset-confirm/:token': [resetConfirm], // was reset-password
35
37
  'post /api/invite-instructions': [inviteInstructions],
36
38
  'post /api/resend-instructions': [resendInstructions],
37
39
  'get /api/invite-pre-confirm/:token': [invitePreConfirm],
@@ -555,7 +557,7 @@ export function tokenCreate(modelName, id) {
555
557
  })
556
558
  }
557
559
 
558
- export function tokenParse(token, modelName, maxAgeMs = 1000 * 60 * 60 * 24) {
560
+ export function tokenParse(token, modelName, maxAgeMs = resetMaxAge) {
559
561
  let [hash, modelNameParsed, id, time] = (token || '').split(':')
560
562
  if (!hash || !id || !time) {
561
563
  throw { title: 'error', detail: 'Sorry your code is invalid.' }
@@ -597,7 +599,7 @@ export async function tokenConfirmForSingleTenant(req, isReset) {
597
599
  const { password, password2, ...userData } = req.body
598
600
  const tokenName = isReset ? 'resetToken' : 'inviteToken'
599
601
  const desktop = req.query.desktop
600
- const _id = db.id(auth.tokenParse(token, 'user'))
602
+ const _id = db.id(auth.tokenParse(token, 'user', isReset ? resetMaxAge : inviteMaxAge))
601
603
  await auth.passwordValidate(password, password2)
602
604
 
603
605
  // Find the inviteToken, but bypass hooks because inviteToken may = true in a potential afterFind hook.
@@ -622,7 +624,7 @@ export async function tokenConfirmForSingleTenant(req, isReset) {
622
624
  export async function tokenConfirmForMultiTenant(req) {
623
625
  const token = req.params.token
624
626
  const desktop = req.query.desktop
625
- const companyId = db.id(auth.tokenParse(token, 'company'))
627
+ const companyId = db.id(auth.tokenParse(token, 'company', inviteMaxAge))
626
628
 
627
629
  // Find the invite (we need to bypass hooks as inviteToken maybe true in an afterFind hook)
628
630
  const company = await db.company._findOne({ _id: companyId, 'invites.inviteToken': token }, { projection: { invites: 1, users: 1 } })
@@ -65,7 +65,6 @@ export function ResetPassword({ className, elements, redirectTo, config }: reset
65
65
  const [state, setState] = useState(() => ({
66
66
  password: '',
67
67
  password2: '',
68
- token: params.token,
69
68
  errors: [] as Errors,
70
69
  }))
71
70
 
@@ -77,7 +76,7 @@ export function ResetPassword({ className, elements, redirectTo, config }: reset
77
76
  async function onSubmit (event: React.FormEvent<HTMLFormElement>) {
78
77
  try {
79
78
  if (isLoading) return
80
- const data = await request('post /api/reset-password', state, event, setIsLoading, setState)
79
+ const data = await request(`post /api/reset-confirm/${params.token}`, state, event, setIsLoading, setState)
81
80
  setStore((s) => ({ ...getSignoutStoreFn(s, getInitialStore()), ...data }))
82
81
  setTimeout(() => navigate(redirectTo || '/'), 10) // wait for setStore
83
82
  } catch (e) {
@@ -10,7 +10,7 @@ type CommonProps = {
10
10
  rowClassName?: string
11
11
  }
12
12
  export type FilterType = (
13
- | FieldProps & CommonProps
13
+ | (FieldProps & CommonProps & { options?: never }) // Makes TS error when options are passed without type: 'select'
14
14
  | ({ type: 'select' } & SelectProps & CommonProps)
15
15
  )
16
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitro-web",
3
- "version": "0.3.26",
3
+ "version": "0.3.28",
4
4
  "repository": "github:boycce/nitro-web",
5
5
  "homepage": "https://boycce.github.io/nitro-web/",
6
6
  "description": "Nitro is a battle-tested, modular base project to turbocharge your projects, styled using Tailwind 🚀",
@@ -57,12 +57,12 @@
57
57
  "passport-jwt": "^4.0.1",
58
58
  "passport-local": "^1.0.0",
59
59
  "sort-route-addresses-nodeps": "0.0.4",
60
- "standard-version": "github:boycce/standard-version",
61
60
  "tailwind-merge": "^2.6.0",
62
61
  "tldts": "^7.0.30"
63
62
  },
64
63
  "devDependencies": {
65
- "@typescript-eslint/eslint-plugin": "^8.18.1"
64
+ "@typescript-eslint/eslint-plugin": "^8.18.1",
65
+ "standard-version": "github:boycce/standard-version"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "@stripe/stripe-js": "^1.34.0",
@@ -14,12 +14,13 @@ let nodemailerMailgun = undefined
14
14
  const _dirname = dirname(fileURLToPath(import.meta.url)) + '/'
15
15
 
16
16
  export const requiredEmailConfigKeys = ['baseUrl', 'emailFrom', 'name', 'env']
17
- export const optionalEmailConfigKeys = ['emailReplyTo', 'emailTestMode', 'mailgunDomain', 'mailgunKey']
17
+ export const optionalEmailConfigKeys = ['emailRedirectTo', 'emailReplyTo', 'emailTestMode', 'mailgunDomain', 'mailgunKey']
18
18
 
19
19
 
20
20
  /**
21
21
  * Sends an email using a predefined template, with optional data/or recipientVariables
22
- * @typedef {{ baseUrl?: string, emailFrom?: string, mailgunDomain?: string, mailgunKey?: string, name?: string }} Config
22
+ * @typedef {{ baseUrl?: string, emailFrom?: string, env?: string, mailgunDomain?: string, mailgunKey?: string,
23
+ * name?: string, pwd?: string, emailRedirectTo?: string }} Config
23
24
  *
24
25
  * @param {object} opts
25
26
  * @param {string} opts.template - Template name or raw HTML, e.g., 'reset-password'
@@ -109,10 +110,17 @@ export async function sendEmail({
109
110
  }
110
111
  }
111
112
 
113
+ // Send everything to emailRedirectTo instead, keeping the first recipient's variables
114
+ const redirectTo = config.emailRedirectTo
115
+ const others = recipients.length - 1
116
+ if (redirectTo) {
117
+ recipientVariables = { [getNameEmail(redirectTo)[1]]: recipientVariables[getNameEmail(to)[1]] }
118
+ }
119
+
112
120
  let settings = {
113
121
  attachments: attachments,
114
- cc: cc,
115
- bcc: bcc,
122
+ cc: redirectTo ? undefined : cc,
123
+ bcc: redirectTo ? undefined : bcc,
116
124
  emailTemplateDir: getDirectories(path, config.pwd).emailTemplateDir,
117
125
  from: from,
118
126
  isDev: config.env === 'development',
@@ -120,9 +128,10 @@ export async function sendEmail({
120
128
  replyTo: replyTo,
121
129
  skipCssInline: skipCssInline,
122
130
  subject: subject,
131
+ subjectPrefix: redirectTo ? `[redirected → ${getNameEmail(to)[1]}${others ? ` +${others}` : ''}] ` : '',
123
132
  template: template,
124
133
  test: isTest,
125
- to: to,
134
+ to: redirectTo || to,
126
135
  swigData: swigData,
127
136
  }
128
137
 
@@ -149,7 +158,7 @@ async function getTemplate(settings) {
149
158
  let template = nunjucksEnv.getTemplate(templateName + '.html', true)
150
159
  let html = template.render(settings.swigData || {})
151
160
  // Inline CSS
152
- if (!settings.skipCssInline || !settings.test) {
161
+ if (!settings.skipCssInline) {
153
162
  try {
154
163
  // First try to inline the CSS from the user templates directory
155
164
  html = await inlineCssForPath(html, settings.emailTemplateDir)
@@ -203,6 +212,8 @@ function processTemplate(settings, html) {
203
212
  if (foundSubject) settings.subject = foundSubject[1]
204
213
  else throw new Error('Sendmail: please pass `subject` or set it in the template')
205
214
  }
215
+ // Name the intended recipients on redirected emails
216
+ if (settings.subjectPrefix) settings.subject = settings.subjectPrefix + settings.subject
206
217
  return html
207
218
  }
208
219
 
@@ -0,0 +1,30 @@
1
+ import { test } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { sendEmail } from '../server/email/index.js'
4
+
5
+ const redirect = { emailRedirectTo: 'Dev<dev@example.test>' }
6
+
7
+ test('sendEmail redirects in test mode too, keeping the intended recipient\'s variables and prefixing the subject', async () => {
8
+ // Development test mode logs what would be sent; capture that instead of posting to Mailgun
9
+ const info = console.info
10
+ const logged = []
11
+ console.info = (...args) => logged.push(args)
12
+ try {
13
+ const html = await sendEmail({
14
+ config: { ...redirect, baseUrl: 'http://localhost:3000', emailFrom: 'Nitro<support@nitro.test>', name: 'nitro',
15
+ env: 'development', emailTestMode: true, pwd: process.cwd() },
16
+ template: '<p>%recipient.greet%, this went to %recipient.email%</p>',
17
+ subject: 'Welcome %recipient.name%',
18
+ to: 'Jane<jane@customer.test>',
19
+ cc: 'cc@customer.test',
20
+ })
21
+ assert.equal(html, '<p>Hi Jane, this went to jane@customer.test</p>')
22
+ const sent = logged.find((args) => args[0] === 'Test mode: sendEmail mailgunOpts')?.[1]
23
+ assert.ok(sent, 'logged the message it would send')
24
+ assert.equal(sent.to, 'Dev<dev@example.test>')
25
+ assert.equal(sent.cc, undefined)
26
+ assert.equal(sent.subject, '[redirected → jane@customer.test +1] Welcome Jane')
27
+ } finally {
28
+ console.info = info
29
+ }
30
+ })
@@ -115,7 +115,7 @@ export const routes: {
115
115
  'post /api/signin': (typeof signin)[];
116
116
  'post /api/signup': (typeof signup)[];
117
117
  'post /api/reset-instructions': (typeof resetInstructions)[];
118
- 'post /api/reset-confirm': (typeof resetConfirm)[];
118
+ 'post /api/reset-confirm/:token': (typeof resetConfirm)[];
119
119
  'post /api/invite-instructions': (typeof inviteInstructions)[];
120
120
  'post /api/resend-instructions': (typeof resendInstructions)[];
121
121
  'get /api/invite-pre-confirm/:token': (typeof invitePreConfirm)[];
@@ -1 +1 @@
1
- {"version":3,"file":"auth.api.d.ts","sourceRoot":"","sources":["../../../components/auth/auth.api.js"],"names":[],"mappings":"AAqLA,qEAUC;AAED,qEAwEC;AAED,qEAGC;AAED,gEAMC;AAED,iEAgBC;AAED,oEAGC;AAED,oEA8BC;AAED,gEAgCC;AAID,qGAkDC;AAED;;;GAOC;AAED;;GAKC;AAED;;;;GAIG;AACH,qCAHW,MAAM,GACJ,MAAM,GAAC,SAAS,CAI5B;AAID;;;;;;;;;GASG;AACH,8EAPG;IAA0B,QAAQ,GAA1B,MAAM;IACY,SAAS,GAA3B,MAAM;IACY,OAAO,GAAzB,MAAM;CACd,YAAQ,MAAM,+BACN,OAAO,GACL,OAAO,CAAC,MAAM,CAAC,CAqE3B;AAED,kFAiBC;AAED,mEAOC;AAED,+EAWC;AAED,sHAiBC;AAED;;;GAEC;AAED;;;GA0BC;AAED;;;;;;;;;GAmCC;AAED;;;;;;;;;;;;;;GAcG;AACH,mGAZG;IAAsD,IAAI,EAAlD,OAAO,GAAG,QAAQ,GAAG,eAAe;IACpB,EAAE,EAAlB,MAAM;IAKH,OAAO,EAJV;QACN,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,EAAE,MAAM,CAAC;QACtB,CAAK,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB;IAC0B,YAAY;IACZ,eAAe;IACjB,OAAO,GAAxB,MAAM;CACd,GAAU,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;CAAC,CAAC,CAmEtE;AAED,0CAEC;AAED,8DA8BC;AAED,gFAKC;;;;;;;;;;;;;;;;;;;;AAnvBD;;;;;;;;;;;;;;;EAiBC;AA0ED,0DAEC;AAgCD,mDAEC;AAtBD,iDAkBC;AA5BD,2DAQC;AA0BD,2DAwBC;AAtID,0EAsEC"}
1
+ {"version":3,"file":"auth.api.d.ts","sourceRoot":"","sources":["../../../components/auth/auth.api.js"],"names":[],"mappings":"AAuLA,qEAUC;AAED,qEAwEC;AAED,qEAGC;AAED,gEAMC;AAED,iEAgBC;AAED,oEAGC;AAED,oEA8BC;AAED,gEAgCC;AAID,qGAkDC;AAED;;;GAOC;AAED;;GAKC;AAED;;;;GAIG;AACH,qCAHW,MAAM,GACJ,MAAM,GAAC,SAAS,CAI5B;AAID;;;;;;;;;GASG;AACH,8EAPG;IAA0B,QAAQ,GAA1B,MAAM;IACY,SAAS,GAA3B,MAAM;IACY,OAAO,GAAzB,MAAM;CACd,YAAQ,MAAM,+BACN,OAAO,GACL,OAAO,CAAC,MAAM,CAAC,CAqE3B;AAED,kFAiBC;AAED,mEAOC;AAED,+EAWC;AAED,sHAiBC;AAED;;;GAEC;AAED;;;GA0BC;AAED;;;;;;;;;GAmCC;AAED;;;;;;;;;;;;;;GAcG;AACH,mGAZG;IAAsD,IAAI,EAAlD,OAAO,GAAG,QAAQ,GAAG,eAAe;IACpB,EAAE,EAAlB,MAAM;IAKH,OAAO,EAJV;QACN,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,EAAE,MAAM,CAAC;QACtB,CAAK,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB;IAC0B,YAAY;IACZ,eAAe;IACjB,OAAO,GAAxB,MAAM;CACd,GAAU,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;CAAC,CAAC,CAmEtE;AAED,0CAEC;AAED,8DA8BC;AAED,gFAKC;;;;;;;;;;;;;;;;;;;;AAnvBD;;;;;;;;;;;;;;;EAiBC;AA0ED,0DAEC;AAgCD,mDAEC;AAtBD,iDAkBC;AA5BD,2DAQC;AA0BD,2DAwBC;AAtID,0EAsEC"}
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Sends an email using a predefined template, with optional data/or recipientVariables
3
- * @typedef {{ baseUrl?: string, emailFrom?: string, mailgunDomain?: string, mailgunKey?: string, name?: string }} Config
3
+ * @typedef {{ baseUrl?: string, emailFrom?: string, env?: string, mailgunDomain?: string, mailgunKey?: string,
4
+ * name?: string, pwd?: string, emailRedirectTo?: string }} Config
4
5
  *
5
6
  * @param {object} opts
6
7
  * @param {string} opts.template - Template name or raw HTML, e.g., 'reset-password'
@@ -47,8 +48,11 @@ export const optionalEmailConfigKeys: string[];
47
48
  export type Config = {
48
49
  baseUrl?: string;
49
50
  emailFrom?: string;
51
+ env?: string;
50
52
  mailgunDomain?: string;
51
53
  mailgunKey?: string;
52
54
  name?: string;
55
+ pwd?: string;
56
+ emailRedirectTo?: string;
53
57
  };
54
58
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../server/email/index.js"],"names":[],"mappings":"AAmBA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,4JAhBG;IAAqB,QAAQ,EAArB,MAAM;IACO,EAAE,EAAf,MAAM;IACO,MAAM,EAAnB,MAAM;IACQ,EAAE,GAAhB,MAAM;IACQ,GAAG,GAAjB,MAAM;IACQ,IAAI,GAAlB,MAAM;IACQ,QAAQ,GAAtB,MAAM;IACQ,IAAI,GAAlB,MAAM;IACQ,OAAO,GAArB,MAAM;IACQ,kBAAkB,GAAhC,MAAM;IACQ,OAAO,GAArB,MAAM;IACS,aAAa,GAA5B,OAAO;IACQ,IAAI,GAAnB,OAAO;IAC8D,WAAW,GAAhF;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE;CACrE,GAAU,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CA8F/B;AArHD,+CAA8E;AAC9E,+CA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