@frozencrow/create-api 0.6.0 → 0.6.2
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/bin/create-api.js +45 -7
- package/lib/generate-service.js +4 -1
- package/package.json +3 -2
- package/template/Dockerfile +5 -0
- package/template/config/custom-environment-variables.json +10 -1
- package/template/config/production.json +1 -0
- package/template/docker-compose.yml +4 -0
- package/template/package.json +2 -2
- package/template/src/services/widgets/widgets.schema.ts +4 -1
- package/template/test/widgets.test.ts +55 -0
package/bin/create-api.js
CHANGED
|
@@ -58,6 +58,31 @@ function copyDir(src, dest) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// npm names may contain `.` and `~`, but MongoDB database names can't contain
|
|
62
|
+
// `.`, and compose project names allow only [a-z0-9_-] starting with a letter or
|
|
63
|
+
// digit. Derive one safe slug for both.
|
|
64
|
+
function toSlug(name) {
|
|
65
|
+
const slug = name.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^[^a-z0-9]+/, '')
|
|
66
|
+
return slug || 'app'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// MongoDB database names must be under 64 bytes; leave room for the `-test` suffix.
|
|
70
|
+
const MAX_DB_BASE_LENGTH = 58
|
|
71
|
+
|
|
72
|
+
function updateJson(file, mutate) {
|
|
73
|
+
if (!fs.existsSync(file)) return
|
|
74
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
75
|
+
mutate(data)
|
|
76
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 4) + '\n')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function replaceInFile(file, replacements) {
|
|
80
|
+
if (!fs.existsSync(file)) return
|
|
81
|
+
let text = fs.readFileSync(file, 'utf8')
|
|
82
|
+
for (const [from, to] of replacements) text = text.replace(from, to)
|
|
83
|
+
fs.writeFileSync(file, text)
|
|
84
|
+
}
|
|
85
|
+
|
|
61
86
|
function ask(question) {
|
|
62
87
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
63
88
|
return new Promise((resolve) => rl.question(question, (answer) => {
|
|
@@ -142,14 +167,27 @@ async function main() {
|
|
|
142
167
|
pkg.name = pkgName
|
|
143
168
|
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4) + '\n')
|
|
144
169
|
|
|
145
|
-
// 2.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
170
|
+
// 2. Replace the template's `my-api` everywhere it appears, so each project
|
|
171
|
+
// gets its own databases and compose project instead of sharing the template's.
|
|
172
|
+
const slug = toSlug(pkgName)
|
|
173
|
+
const dbName = slug.slice(0, MAX_DB_BASE_LENGTH)
|
|
174
|
+
|
|
175
|
+
updateJson(path.join(targetDir, 'config', 'default.json'), (cfg) => {
|
|
149
176
|
cfg.appName = pkgName
|
|
150
|
-
cfg.mongodb = `mongodb://localhost:27017/${
|
|
151
|
-
|
|
152
|
-
|
|
177
|
+
cfg.mongodb = `mongodb://localhost:27017/${dbName}`
|
|
178
|
+
})
|
|
179
|
+
updateJson(path.join(targetDir, 'config', 'test.json'), (cfg) => {
|
|
180
|
+
cfg.appName = `${pkgName} (test)`
|
|
181
|
+
cfg.mongodb = `mongodb://localhost:27017/${dbName}-test`
|
|
182
|
+
})
|
|
183
|
+
replaceInFile(path.join(targetDir, 'docker-compose.yml'), [
|
|
184
|
+
[/^name: my-api$/m, `name: ${slug}`],
|
|
185
|
+
['mongodb://mongodb:27017/my-api', `mongodb://mongodb:27017/${dbName}`]
|
|
186
|
+
])
|
|
187
|
+
replaceInFile(path.join(targetDir, 'README.md'), [[/^# My API$/m, `# ${pkgName}`]])
|
|
188
|
+
replaceInFile(path.join(targetDir, 'public', 'index.html'), [
|
|
189
|
+
['<title>My API</title>', `<title>${pkgName}</title>`]
|
|
190
|
+
])
|
|
153
191
|
|
|
154
192
|
// 3. Generate a strong secret into config/local.json (git-ignored). The config
|
|
155
193
|
// module (config/*.json) loads local.json last, so this overrides the default
|
package/lib/generate-service.js
CHANGED
|
@@ -74,8 +74,11 @@ export const ${n.camel}DataResolver = resolve<${n.Pascal}, HookContext>({
|
|
|
74
74
|
updatedAt: async () => Date.now()
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
// Patchable fields: the client's own fields, plus updatedAt (set by the patch
|
|
78
|
+
// resolver, which runs before validation). organizationId and createdAt are
|
|
79
|
+
// deliberately left out: they are set once on create and must never change.
|
|
77
80
|
export const ${n.camel}PatchSchema = Type.Partial(
|
|
78
|
-
Type.Pick(${n.camel}Schema, ['name', '
|
|
81
|
+
Type.Pick(${n.camel}Schema, ['name', 'updatedAt']),
|
|
79
82
|
{ $id: '${n.Pascal}Patch' }
|
|
80
83
|
)
|
|
81
84
|
export type ${n.Pascal}Patch = Static<typeof ${n.camel}PatchSchema>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frozencrow/create-api",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Scaffold a new API project powered by @frozencrow/api-core",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Eric Uldall",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"template"
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
|
-
"build": "node -e \"process.exit(0)\""
|
|
35
|
+
"build": "node -e \"process.exit(0)\"",
|
|
36
|
+
"test": "node --test test/scaffold.test.js"
|
|
36
37
|
},
|
|
37
38
|
"publishConfig": {
|
|
38
39
|
"access": "public"
|
package/template/Dockerfile
CHANGED
|
@@ -12,6 +12,11 @@ RUN npm run compile
|
|
|
12
12
|
# Production stage
|
|
13
13
|
FROM node:20-alpine
|
|
14
14
|
|
|
15
|
+
# Run as production: api-core refuses to boot with a missing/weak/placeholder
|
|
16
|
+
# AUTH_SECRET only when NODE_ENV=production. (docker-compose.yml overrides this
|
|
17
|
+
# to `development` for local use.)
|
|
18
|
+
ENV NODE_ENV=production
|
|
19
|
+
|
|
15
20
|
WORKDIR /app
|
|
16
21
|
|
|
17
22
|
COPY package*.json ./
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
{
|
|
2
|
-
"port":
|
|
2
|
+
"port": {
|
|
3
|
+
"__name": "PORT",
|
|
4
|
+
"__format": "number"
|
|
5
|
+
},
|
|
3
6
|
"mongodb": "MONGODB_URI",
|
|
4
7
|
"clientHost": "CLIENT_HOST",
|
|
5
8
|
"origins": {
|
|
@@ -9,6 +12,12 @@
|
|
|
9
12
|
"authentication": {
|
|
10
13
|
"secret": "AUTH_SECRET"
|
|
11
14
|
},
|
|
15
|
+
"socketio": {
|
|
16
|
+
"maxHttpBufferSize": {
|
|
17
|
+
"__name": "SOCKETIO_MAX_HTTP_BUFFER_SIZE",
|
|
18
|
+
"__format": "number"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
12
21
|
"mail": {
|
|
13
22
|
"host": "MAIL_HOST",
|
|
14
23
|
"port": {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|
package/template/package.json
CHANGED
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"test": "npm run mocha"
|
|
17
17
|
},
|
|
18
18
|
"overrides": {
|
|
19
|
-
"config": "3.3.
|
|
19
|
+
"config": "3.3.12"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@frozencrow/api-core": "^0.6.
|
|
22
|
+
"@frozencrow/api-core": "^0.6.2",
|
|
23
23
|
"@feathersjs/configuration": "^5.0.37",
|
|
24
24
|
"@feathersjs/feathers": "^5.0.37",
|
|
25
25
|
"@feathersjs/mongodb": "^5.0.37",
|
|
@@ -46,8 +46,11 @@ export const widgetDataResolver = resolve<Widget, HookContext>({
|
|
|
46
46
|
updatedAt: async () => Date.now()
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
+
// Patchable fields: the client's own fields, plus updatedAt (set by the patch
|
|
50
|
+
// resolver, which runs before validation). organizationId and createdAt are
|
|
51
|
+
// deliberately left out: they are set once on create and must never change.
|
|
49
52
|
export const widgetPatchSchema = Type.Partial(
|
|
50
|
-
Type.Pick(widgetSchema, ['name', '
|
|
53
|
+
Type.Pick(widgetSchema, ['name', 'updatedAt']),
|
|
51
54
|
{ $id: 'WidgetPatch' }
|
|
52
55
|
)
|
|
53
56
|
export type WidgetPatch = Static<typeof widgetPatchSchema>
|
|
@@ -57,6 +57,61 @@ describe('widgets service', () => {
|
|
|
57
57
|
assert.ok(widget.organizationId, 'widget is scoped to an organization')
|
|
58
58
|
})
|
|
59
59
|
|
|
60
|
+
it('keeps a widget in its organization when a patch names another one', async () => {
|
|
61
|
+
const authHeaders = (token: string) => ({ 'Content-Type': 'application/json', Authorization: `Bearer ${token}` })
|
|
62
|
+
|
|
63
|
+
// A second user, with their own organization.
|
|
64
|
+
const creds = { email: `w2-${Date.now()}@example.com`, password: 'supersecret' }
|
|
65
|
+
await fetch(`${appUrl}/users`, {
|
|
66
|
+
method: 'POST',
|
|
67
|
+
headers: { 'Content-Type': 'application/json' },
|
|
68
|
+
body: JSON.stringify(creds)
|
|
69
|
+
})
|
|
70
|
+
const other = await asJson(
|
|
71
|
+
await fetch(`${appUrl}/authentication`, {
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers: { 'Content-Type': 'application/json' },
|
|
74
|
+
body: JSON.stringify({ strategy: 'local', ...creds })
|
|
75
|
+
})
|
|
76
|
+
)
|
|
77
|
+
const otherOrg = String(other.user.activeOrganization)
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const widget = await asJson(
|
|
81
|
+
await fetch(`${appUrl}/widgets`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: authHeaders(accessToken),
|
|
84
|
+
body: JSON.stringify({ name: 'Mine' })
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
const ownOrg = String(widget.organizationId)
|
|
88
|
+
assert.notStrictEqual(ownOrg, otherOrg)
|
|
89
|
+
|
|
90
|
+
for (const data of [{ organizationId: otherOrg }, { createdAt: 1 }]) {
|
|
91
|
+
const res = await fetch(`${appUrl}/widgets/${widget._id}`, {
|
|
92
|
+
method: 'PATCH',
|
|
93
|
+
headers: authHeaders(accessToken),
|
|
94
|
+
body: JSON.stringify(data)
|
|
95
|
+
})
|
|
96
|
+
assert.ok(res.status === 400 || res.status === 403, `${JSON.stringify(data)}: expected 400/403, got ${res.status}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const stored: any = await app.service('widgets').get(widget._id)
|
|
100
|
+
assert.strictEqual(String(stored.organizationId), ownOrg, 'widget stayed in its organization')
|
|
101
|
+
assert.strictEqual(stored.createdAt, widget.createdAt, 'createdAt unchanged')
|
|
102
|
+
|
|
103
|
+
const otherList = await asJson(await fetch(`${appUrl}/widgets`, { headers: authHeaders(other.accessToken) }))
|
|
104
|
+
const otherIds = (otherList.data || otherList).map((w: any) => String(w._id))
|
|
105
|
+
assert.ok(!otherIds.includes(String(widget._id)), 'not visible to the other organization')
|
|
106
|
+
} finally {
|
|
107
|
+
try {
|
|
108
|
+
await app.service('users').remove(other.user._id)
|
|
109
|
+
} catch {
|
|
110
|
+
/* ignore cleanup errors */
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
60
115
|
it('rejects unauthenticated widget access', async () => {
|
|
61
116
|
const res = await fetch(`${appUrl}/widgets`)
|
|
62
117
|
assert.ok(res.status === 401 || res.status === 403, `expected auth error, got ${res.status}`)
|