@tscircuit/fake-snippets 0.0.6 → 0.0.8
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/bun-tests/fake-snippets-api/fixtures/get-test-server.ts +2 -5
- package/bun-tests/fake-snippets-api/routes/package_files/create.test.ts +375 -0
- package/bun-tests/fake-snippets-api/routes/package_files/download.test.ts +248 -0
- package/bun-tests/fake-snippets-api/routes/package_files/get.test.ts +220 -0
- package/bun-tests/fake-snippets-api/routes/package_files/list.test.ts +204 -0
- package/bun-tests/fake-snippets-api/routes/package_releases/get.test.ts +0 -1
- package/bun-tests/fake-snippets-api/routes/packages/{list.test.ts → list-1.test.ts} +3 -55
- package/bun-tests/fake-snippets-api/routes/packages/list-2.test.ts +59 -0
- package/bun-tests/fake-snippets-api/routes/snippets/create.test.ts +34 -1
- package/bun.lock +100 -73
- package/dist/bundle.js +729 -273
- package/fake-snippets-api/lib/db/db-client.ts +58 -50
- package/fake-snippets-api/lib/db/schema.ts +15 -6
- package/fake-snippets-api/lib/package_file/get-package-file-id-from-file-descriptor.ts +168 -0
- package/fake-snippets-api/lib/package_release/find-package-release-id.ts +122 -0
- package/fake-snippets-api/lib/public-mapping/public-map-package.ts +9 -2
- package/fake-snippets-api/routes/api/package_files/create.ts +132 -0
- package/fake-snippets-api/routes/api/package_files/download.ts +70 -153
- package/fake-snippets-api/routes/api/package_files/get.ts +24 -5
- package/fake-snippets-api/routes/api/package_files/list.ts +16 -28
- package/fake-snippets-api/routes/api/snippets/create.ts +123 -29
- package/index.html +12 -1
- package/package.json +10 -9
- package/playwright-tests/profile-page.spec.ts +59 -0
- package/playwright-tests/snapshots/profile-page.spec.ts-profile-page-before-delete.png +0 -0
- package/playwright-tests/snapshots/profile-page.spec.ts-profile-page-delete-dialog.png +0 -0
- package/playwright-tests/snapshots/profile-page.spec.ts-profile-page-dropdown-open.png +0 -0
- package/scripts/generate-image-sizes.ts +0 -1
- package/scripts/generate_bundle_stats.js +22 -6
- package/src/components/AiChatInterface.tsx +8 -0
- package/src/components/Analytics.tsx +1 -1
- package/src/components/CodeAndPreview.tsx +9 -3
- package/src/components/CreateNewSnippetWithAiHero.tsx +6 -2
- package/src/components/EditorNav.tsx +4 -0
- package/src/components/Footer.tsx +1 -1
- package/src/components/Header.tsx +7 -10
- package/src/components/HeaderLogin.tsx +1 -1
- package/src/components/PreviewContent.tsx +4 -1
- package/src/components/SnippetList.tsx +71 -0
- package/src/lib/templates/blinking-led-board-template.ts +2 -1
- package/src/pages/dashboard.tsx +19 -44
- package/src/pages/editor.tsx +1 -1
- package/src/pages/landing.tsx +8 -16
- package/src/pages/quickstart.tsx +9 -9
- package/src/pages/user-profile.tsx +50 -3
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { z } from "zod"
|
|
1
|
+
import type { z } from "zod"
|
|
2
|
+
import { hoist } from "zustand-hoist"
|
|
3
|
+
import { createStore } from "zustand/vanilla"
|
|
5
4
|
|
|
5
|
+
import { combine } from "zustand/middleware"
|
|
6
6
|
import {
|
|
7
|
+
type Account,
|
|
8
|
+
type AccountSnippet,
|
|
9
|
+
type LoginPage,
|
|
10
|
+
type Order,
|
|
11
|
+
type OrderFile,
|
|
12
|
+
type Package,
|
|
13
|
+
type PackageFile,
|
|
14
|
+
type PackageRelease,
|
|
15
|
+
type Session,
|
|
16
|
+
type Snippet,
|
|
7
17
|
databaseSchema,
|
|
8
|
-
Snippet,
|
|
9
|
-
Session,
|
|
10
|
-
LoginPage,
|
|
11
|
-
Account,
|
|
12
|
-
type DatabaseSchema,
|
|
13
|
-
snippetSchema,
|
|
14
|
-
Order,
|
|
15
|
-
OrderFile,
|
|
16
|
-
AccountSnippet,
|
|
17
18
|
packageReleaseSchema,
|
|
18
|
-
packageSchema,
|
|
19
|
-
|
|
20
|
-
PackageRelease,
|
|
19
|
+
type packageSchema,
|
|
20
|
+
snippetSchema,
|
|
21
21
|
} from "./schema.ts"
|
|
22
|
-
import { combine } from "zustand/middleware"
|
|
23
22
|
import { seed as seedFn } from "./seed"
|
|
24
23
|
|
|
25
24
|
export const createDatabase = ({ seed }: { seed?: boolean } = {}) => {
|
|
@@ -34,7 +33,7 @@ export type DbClient = ReturnType<typeof createDatabase>
|
|
|
34
33
|
|
|
35
34
|
const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
36
35
|
addOrder: (order: Omit<Order, "order_id">): Order => {
|
|
37
|
-
|
|
36
|
+
const newOrder = { order_id: `order_${get().idCounter + 1}`, ...order }
|
|
38
37
|
set((state) => {
|
|
39
38
|
return {
|
|
40
39
|
orders: [...state.orders, newOrder],
|
|
@@ -179,13 +178,13 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
179
178
|
)
|
|
180
179
|
},
|
|
181
180
|
updateSnippet: (
|
|
182
|
-
|
|
181
|
+
snippetId: string,
|
|
183
182
|
updates: Partial<Snippet>,
|
|
184
183
|
): Snippet | undefined => {
|
|
185
184
|
let updatedSnippet: Snippet | undefined
|
|
186
185
|
set((state) => {
|
|
187
186
|
const snippetIndex = state.snippets.findIndex(
|
|
188
|
-
(snippet) => snippet.snippet_id ===
|
|
187
|
+
(snippet) => snippet.snippet_id === snippetId,
|
|
189
188
|
)
|
|
190
189
|
if (snippetIndex === -1) {
|
|
191
190
|
return state
|
|
@@ -201,16 +200,16 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
201
200
|
})
|
|
202
201
|
return updatedSnippet
|
|
203
202
|
},
|
|
204
|
-
getSnippetById: (
|
|
203
|
+
getSnippetById: (snippetId: string): Snippet | undefined => {
|
|
205
204
|
const state = get()
|
|
206
205
|
const snippet = state.snippets.find(
|
|
207
|
-
(snippet) => snippet.snippet_id ===
|
|
206
|
+
(snippet) => snippet.snippet_id === snippetId,
|
|
208
207
|
)
|
|
209
208
|
if (!snippet) return undefined
|
|
210
209
|
return {
|
|
211
210
|
...snippet,
|
|
212
211
|
star_count: state.accountSnippets.filter(
|
|
213
|
-
(as) => as.snippet_id ===
|
|
212
|
+
(as) => as.snippet_id === snippetId && as.has_starred,
|
|
214
213
|
).length,
|
|
215
214
|
}
|
|
216
215
|
},
|
|
@@ -231,10 +230,10 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
231
230
|
).length,
|
|
232
231
|
}))
|
|
233
232
|
},
|
|
234
|
-
deleteSnippet: (
|
|
233
|
+
deleteSnippet: (snippetId: string): boolean => {
|
|
235
234
|
let deleted = false
|
|
236
235
|
set((state) => {
|
|
237
|
-
const index = state.snippets.findIndex((s) => s.snippet_id ===
|
|
236
|
+
const index = state.snippets.findIndex((s) => s.snippet_id === snippetId)
|
|
238
237
|
if (index !== -1) {
|
|
239
238
|
state.snippets.splice(index, 1)
|
|
240
239
|
deleted = true
|
|
@@ -276,32 +275,29 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
276
275
|
}))
|
|
277
276
|
return newLoginPage
|
|
278
277
|
},
|
|
279
|
-
getLoginPage: (
|
|
278
|
+
getLoginPage: (loginPageId: string): LoginPage | undefined => {
|
|
280
279
|
const state = get()
|
|
281
|
-
return state.loginPages.find((lp) => lp.login_page_id ===
|
|
280
|
+
return state.loginPages.find((lp) => lp.login_page_id === loginPageId)
|
|
282
281
|
},
|
|
283
|
-
updateLoginPage: (
|
|
284
|
-
login_page_id: string,
|
|
285
|
-
updates: Partial<LoginPage>,
|
|
286
|
-
): void => {
|
|
282
|
+
updateLoginPage: (loginPageId: string, updates: Partial<LoginPage>): void => {
|
|
287
283
|
set((state) => ({
|
|
288
284
|
loginPages: state.loginPages.map((lp) =>
|
|
289
|
-
lp.login_page_id ===
|
|
285
|
+
lp.login_page_id === loginPageId ? { ...lp, ...updates } : lp,
|
|
290
286
|
),
|
|
291
287
|
}))
|
|
292
288
|
},
|
|
293
|
-
getAccount: (
|
|
289
|
+
getAccount: (accountId: string): Account | undefined => {
|
|
294
290
|
const state = get()
|
|
295
|
-
return state.accounts.find((account) => account.account_id ===
|
|
291
|
+
return state.accounts.find((account) => account.account_id === accountId)
|
|
296
292
|
},
|
|
297
293
|
updateAccount: (
|
|
298
|
-
|
|
294
|
+
accountId: string,
|
|
299
295
|
updates: Partial<Account>,
|
|
300
296
|
): Account | undefined => {
|
|
301
297
|
let updatedAccount: Account | undefined
|
|
302
298
|
set((state) => {
|
|
303
299
|
const accountIndex = state.accounts.findIndex(
|
|
304
|
-
(account) => account.account_id ===
|
|
300
|
+
(account) => account.account_id === accountId,
|
|
305
301
|
)
|
|
306
302
|
if (accountIndex !== -1) {
|
|
307
303
|
updatedAccount = { ...state.accounts[accountIndex] }
|
|
@@ -328,11 +324,11 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
328
324
|
}))
|
|
329
325
|
return newSession
|
|
330
326
|
},
|
|
331
|
-
addStar: (
|
|
327
|
+
addStar: (accountId: string, snippetId: string): AccountSnippet => {
|
|
332
328
|
const now = new Date().toISOString()
|
|
333
329
|
const accountSnippet = {
|
|
334
|
-
account_id,
|
|
335
|
-
snippet_id,
|
|
330
|
+
account_id: accountId,
|
|
331
|
+
snippet_id: snippetId,
|
|
336
332
|
has_starred: true,
|
|
337
333
|
created_at: now,
|
|
338
334
|
updated_at: now,
|
|
@@ -342,19 +338,19 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
342
338
|
}))
|
|
343
339
|
return accountSnippet
|
|
344
340
|
},
|
|
345
|
-
removeStar: (
|
|
341
|
+
removeStar: (accountId: string, snippetId: string): void => {
|
|
346
342
|
set((state) => ({
|
|
347
343
|
accountSnippets: state.accountSnippets.filter(
|
|
348
|
-
(as) => !(as.account_id ===
|
|
344
|
+
(as) => !(as.account_id === accountId && as.snippet_id === snippetId),
|
|
349
345
|
),
|
|
350
346
|
}))
|
|
351
347
|
},
|
|
352
|
-
hasStarred: (
|
|
348
|
+
hasStarred: (accountId: string, snippetId: string): boolean => {
|
|
353
349
|
const state = get()
|
|
354
350
|
return state.accountSnippets.some(
|
|
355
351
|
(as) =>
|
|
356
|
-
as.account_id ===
|
|
357
|
-
as.snippet_id ===
|
|
352
|
+
as.account_id === accountId &&
|
|
353
|
+
as.snippet_id === snippetId &&
|
|
358
354
|
as.has_starred,
|
|
359
355
|
)
|
|
360
356
|
},
|
|
@@ -376,24 +372,24 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
376
372
|
})
|
|
377
373
|
newPackage.latest_package_release_id = packageRelease.package_release_id
|
|
378
374
|
set((state) => ({
|
|
379
|
-
packages: [...state.packages, newPackage],
|
|
375
|
+
packages: [...state.packages, newPackage as Package],
|
|
380
376
|
}))
|
|
381
|
-
return newPackage
|
|
377
|
+
return newPackage as Package
|
|
382
378
|
},
|
|
383
|
-
getPackageById: (
|
|
379
|
+
getPackageById: (packageId: string): Package | undefined => {
|
|
384
380
|
const state = get()
|
|
385
|
-
const pkg = state.packages.find((pkg) => pkg.package_id ===
|
|
381
|
+
const pkg = state.packages.find((pkg) => pkg.package_id === packageId)
|
|
386
382
|
if (!pkg) return undefined
|
|
387
383
|
return {
|
|
388
384
|
...pkg,
|
|
389
385
|
}
|
|
390
386
|
},
|
|
391
387
|
getPackageReleaseById: (
|
|
392
|
-
|
|
388
|
+
packageReleaseId: string,
|
|
393
389
|
): PackageRelease | undefined => {
|
|
394
390
|
const state = get()
|
|
395
391
|
return state.packageReleases.find(
|
|
396
|
-
(pr) => pr.package_release_id ===
|
|
392
|
+
(pr) => pr.package_release_id === packageReleaseId,
|
|
397
393
|
)
|
|
398
394
|
},
|
|
399
395
|
addPackageRelease: (
|
|
@@ -417,4 +413,16 @@ const initializer = combine(databaseSchema.parse({}), (set, get) => ({
|
|
|
417
413
|
),
|
|
418
414
|
}))
|
|
419
415
|
},
|
|
416
|
+
addPackageFile: (
|
|
417
|
+
packageFile: Omit<PackageFile, "package_file_id">,
|
|
418
|
+
): PackageFile => {
|
|
419
|
+
const newPackageFile = {
|
|
420
|
+
package_file_id: `package_file_${Date.now()}`,
|
|
421
|
+
...packageFile,
|
|
422
|
+
}
|
|
423
|
+
set((state) => ({
|
|
424
|
+
packageFiles: [...state.packageFiles, newPackageFile],
|
|
425
|
+
}))
|
|
426
|
+
return newPackageFile
|
|
427
|
+
},
|
|
420
428
|
}))
|
|
@@ -124,18 +124,27 @@ export type PackageFile = z.infer<typeof packageFileSchema>
|
|
|
124
124
|
export const packageSchema = z.object({
|
|
125
125
|
package_id: z.string(),
|
|
126
126
|
creator_account_id: z.string(),
|
|
127
|
-
latest_package_release_id: z.string().nullable(),
|
|
128
|
-
latest_version: z.string().nullable(),
|
|
129
|
-
license: z.string().nullable(),
|
|
130
127
|
owner_org_id: z.string(),
|
|
131
128
|
owner_github_username: z.string().nullable(),
|
|
132
|
-
is_source_from_github: z.boolean(),
|
|
133
|
-
description: z.string().nullable(),
|
|
134
129
|
name: z.string(),
|
|
135
130
|
unscoped_name: z.string(),
|
|
136
|
-
|
|
131
|
+
description: z.string().nullable(),
|
|
137
132
|
created_at: z.string().datetime(),
|
|
138
133
|
updated_at: z.string().datetime(),
|
|
134
|
+
is_snippet: z.boolean().default(false),
|
|
135
|
+
is_board: z.boolean().default(false),
|
|
136
|
+
is_package: z.boolean().default(false),
|
|
137
|
+
is_model: z.boolean().default(false),
|
|
138
|
+
is_footprint: z.boolean().default(false),
|
|
139
|
+
is_private: z.boolean().nullable().default(false),
|
|
140
|
+
is_public: z.boolean().nullable().default(true),
|
|
141
|
+
is_unlisted: z.boolean().nullable().default(false),
|
|
142
|
+
is_source_from_github: z.boolean().default(false),
|
|
143
|
+
snippet_type: z.enum(["board", "package", "model", "footprint"]).optional(),
|
|
144
|
+
latest_package_release_id: z.string().nullable(),
|
|
145
|
+
latest_version: z.string().nullable(),
|
|
146
|
+
license: z.string().nullable(),
|
|
147
|
+
star_count: z.number().default(0),
|
|
139
148
|
ai_description: z.string().nullable(),
|
|
140
149
|
})
|
|
141
150
|
export type Package = z.infer<typeof packageSchema>
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { NotFoundError } from "winterspec/middleware"
|
|
2
|
+
import * as ZT from "../db/schema"
|
|
3
|
+
export const getPackageFileIdFromFileDescriptor = async (
|
|
4
|
+
descriptor:
|
|
5
|
+
| { package_file_id: string }
|
|
6
|
+
| { package_release_id: string; file_path: string }
|
|
7
|
+
| { package_id: string; version?: string; file_path: string }
|
|
8
|
+
| { package_name: string; version?: string; file_path: string }
|
|
9
|
+
| { package_name_with_version: string; file_path: string },
|
|
10
|
+
ctx: { db: any },
|
|
11
|
+
): Promise<string> => {
|
|
12
|
+
if ("package_file_id" in descriptor) {
|
|
13
|
+
const packageFile = ctx.db.packageFiles.find(
|
|
14
|
+
(pf: ZT.PackageFile) => pf.package_file_id === descriptor.package_file_id,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
if (!packageFile) {
|
|
18
|
+
throw new NotFoundError("Package file not found")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return descriptor.package_file_id
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if ("package_release_id" in descriptor) {
|
|
25
|
+
const { package_release_id, file_path } = descriptor
|
|
26
|
+
const packageFile = ctx.db.packageFiles.find(
|
|
27
|
+
(pf: ZT.PackageFile) =>
|
|
28
|
+
pf.package_release_id === package_release_id &&
|
|
29
|
+
pf.file_path === file_path,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if (!packageFile) {
|
|
33
|
+
throw new NotFoundError("Package file not found")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return packageFile.package_file_id
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if ("package_id" in descriptor) {
|
|
40
|
+
const { package_id, version, file_path } = descriptor
|
|
41
|
+
|
|
42
|
+
// Verify package exists
|
|
43
|
+
const pkg = ctx.db.packages.find(
|
|
44
|
+
(p: ZT.Package) => p.package_id === package_id,
|
|
45
|
+
)
|
|
46
|
+
if (!pkg) {
|
|
47
|
+
throw new NotFoundError("Package not found")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Find the package release
|
|
51
|
+
let packageRelease
|
|
52
|
+
if (version) {
|
|
53
|
+
packageRelease = ctx.db.packageReleases.find(
|
|
54
|
+
(pr: ZT.PackageRelease) =>
|
|
55
|
+
pr.package_id === package_id && pr.version === version,
|
|
56
|
+
)
|
|
57
|
+
} else {
|
|
58
|
+
packageRelease = ctx.db.packageReleases.find(
|
|
59
|
+
(pr: ZT.PackageRelease) => pr.package_id === package_id && pr.is_latest,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!packageRelease) {
|
|
64
|
+
throw new NotFoundError("Package release not found")
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Find the file
|
|
68
|
+
const packageFile = ctx.db.packageFiles.find(
|
|
69
|
+
(pf: ZT.PackageFile) =>
|
|
70
|
+
pf.package_release_id === packageRelease.package_release_id &&
|
|
71
|
+
pf.file_path === file_path,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
if (!packageFile) {
|
|
75
|
+
throw new NotFoundError("Package file not found")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return packageFile.package_file_id
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if ("package_name" in descriptor) {
|
|
82
|
+
const { package_name, version, file_path } = descriptor
|
|
83
|
+
|
|
84
|
+
// Find the package first
|
|
85
|
+
const pkg = ctx.db.packages.find((p: ZT.Package) => p.name === package_name)
|
|
86
|
+
if (!pkg) {
|
|
87
|
+
throw new NotFoundError(`Package not found: ${package_name}`)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Find the package release
|
|
91
|
+
let packageRelease
|
|
92
|
+
if (version) {
|
|
93
|
+
packageRelease = ctx.db.packageReleases.find(
|
|
94
|
+
(pr: ZT.PackageRelease) =>
|
|
95
|
+
pr.package_id === pkg.package_id && pr.version === version,
|
|
96
|
+
)
|
|
97
|
+
} else {
|
|
98
|
+
packageRelease = ctx.db.packageReleases.find(
|
|
99
|
+
(pr: ZT.PackageRelease) =>
|
|
100
|
+
pr.package_id === pkg.package_id && pr.is_latest,
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!packageRelease) {
|
|
105
|
+
throw new NotFoundError("Package release not found")
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Find the file
|
|
109
|
+
const packageFile = ctx.db.packageFiles.find(
|
|
110
|
+
(pf: ZT.PackageFile) =>
|
|
111
|
+
pf.package_release_id === packageRelease.package_release_id &&
|
|
112
|
+
pf.file_path === file_path,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
if (!packageFile) {
|
|
116
|
+
throw new NotFoundError("Package file not found")
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return packageFile.package_file_id
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if ("package_name_with_version" in descriptor) {
|
|
123
|
+
const { package_name_with_version, file_path } = descriptor
|
|
124
|
+
const packageName = package_name_with_version.split("@")[1]
|
|
125
|
+
const version = package_name_with_version.split("@")[2]
|
|
126
|
+
|
|
127
|
+
// Find the package
|
|
128
|
+
const pkg = ctx.db.packages.find((p: ZT.Package) => p.name === packageName)
|
|
129
|
+
if (!pkg) {
|
|
130
|
+
throw new NotFoundError(`Package not found: ${packageName}`)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Find the package release
|
|
134
|
+
let packageRelease
|
|
135
|
+
if (!version || version === "latest") {
|
|
136
|
+
packageRelease = ctx.db.packageReleases.find(
|
|
137
|
+
(pr: ZT.PackageRelease) =>
|
|
138
|
+
pr.package_id === pkg.package_id && pr.is_latest,
|
|
139
|
+
)
|
|
140
|
+
} else {
|
|
141
|
+
packageRelease = ctx.db.packageReleases.find(
|
|
142
|
+
(pr: ZT.PackageRelease) =>
|
|
143
|
+
pr.package_id === pkg.package_id && pr.version === version,
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!packageRelease) {
|
|
148
|
+
throw new NotFoundError(
|
|
149
|
+
`Package release not found for version: ${version || "latest"}`,
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Find the file
|
|
154
|
+
const packageFile = ctx.db.packageFiles.find(
|
|
155
|
+
(pf: ZT.PackageFile) =>
|
|
156
|
+
pf.package_release_id === packageRelease.package_release_id &&
|
|
157
|
+
pf.file_path === file_path,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
if (!packageFile) {
|
|
161
|
+
throw new NotFoundError(`Package file not found: ${file_path}`)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return packageFile.package_file_id
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
throw new NotFoundError("Invalid package file descriptor")
|
|
168
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Package, PackageRelease } from "fake-snippets-api/lib/db/schema"
|
|
2
|
+
|
|
3
|
+
export const findPackageReleaseId = async (
|
|
4
|
+
params:
|
|
5
|
+
| string
|
|
6
|
+
| {
|
|
7
|
+
package_release_id: string
|
|
8
|
+
}
|
|
9
|
+
| {
|
|
10
|
+
package_name: string
|
|
11
|
+
use_latest_version: true
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
package_name_with_version: string
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
package_release_id?: string | undefined
|
|
18
|
+
package_name_with_version?: string | undefined
|
|
19
|
+
},
|
|
20
|
+
ctx: { db: any },
|
|
21
|
+
): Promise<string | null> => {
|
|
22
|
+
if (typeof params === "string") {
|
|
23
|
+
return findPackageReleaseIdFromPackageNameWithVersion(params, ctx.db)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (
|
|
27
|
+
"package_name_with_version" in params &&
|
|
28
|
+
params.package_name_with_version
|
|
29
|
+
) {
|
|
30
|
+
const { package_name_with_version } = params
|
|
31
|
+
|
|
32
|
+
if (package_name_with_version.endsWith("@latest")) {
|
|
33
|
+
const [packageName] = package_name_with_version.split("@")
|
|
34
|
+
return findPackageReleaseId(
|
|
35
|
+
{
|
|
36
|
+
package_name: packageName!,
|
|
37
|
+
use_latest_version: true,
|
|
38
|
+
},
|
|
39
|
+
ctx,
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
return findPackageReleaseIdFromPackageNameWithVersion(
|
|
43
|
+
package_name_with_version!,
|
|
44
|
+
ctx.db,
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if ("package_release_id" in params && params.package_release_id) {
|
|
49
|
+
const packageRelease = ctx.db.packageReleases.find(
|
|
50
|
+
(pr: PackageRelease) =>
|
|
51
|
+
pr.package_release_id === params.package_release_id,
|
|
52
|
+
)
|
|
53
|
+
if (!packageRelease) {
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
return params.package_release_id
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if ("package_name" in params) {
|
|
60
|
+
const { package_name, use_latest_version } = params
|
|
61
|
+
|
|
62
|
+
if (!use_latest_version) {
|
|
63
|
+
throw new Error("use_latest_version must be true")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const pkg = ctx.db.packages.find(
|
|
67
|
+
(p: Package) => p.name === package_name.replace(/^@/, ""),
|
|
68
|
+
)
|
|
69
|
+
if (!pkg) {
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const packageRelease = ctx.db.packageReleases.find(
|
|
74
|
+
(pr: PackageRelease) => pr.package_id === pkg.package_id && pr.is_latest,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
if (!packageRelease) {
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return packageRelease.package_release_id
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return null
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const findPackageReleaseIdFromPackageNameWithVersion = (
|
|
88
|
+
packageNameWithVersion: string,
|
|
89
|
+
db: any,
|
|
90
|
+
): string | null => {
|
|
91
|
+
if (!packageNameWithVersion) return null
|
|
92
|
+
|
|
93
|
+
const [packageName, version] = packageNameWithVersion
|
|
94
|
+
.replace(/^@/, "")
|
|
95
|
+
.split("@")
|
|
96
|
+
|
|
97
|
+
const pkg = db.packages.find((p: Package) => p.name === packageName)
|
|
98
|
+
if (!pkg) {
|
|
99
|
+
return null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let packageRelease
|
|
103
|
+
|
|
104
|
+
if (!version || version === "latest") {
|
|
105
|
+
packageRelease = db.packageReleases.find(
|
|
106
|
+
(pr: PackageRelease) => pr.package_id === pkg.package_id && pr.is_latest,
|
|
107
|
+
)
|
|
108
|
+
} else {
|
|
109
|
+
packageRelease = db.packageReleases.find(
|
|
110
|
+
(pr: PackageRelease) =>
|
|
111
|
+
pr.package_id === pkg.package_id && pr.version === version,
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!packageRelease) {
|
|
116
|
+
return null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
console.log("packageRelease", packageRelease)
|
|
120
|
+
|
|
121
|
+
return packageRelease.package_release_id
|
|
122
|
+
}
|
|
@@ -17,15 +17,22 @@ export const publicMapPackage = (internal_package: {
|
|
|
17
17
|
updated_at: string
|
|
18
18
|
created_at: string
|
|
19
19
|
ai_description: string | null
|
|
20
|
+
is_snippet: boolean
|
|
21
|
+
is_board: boolean
|
|
22
|
+
is_package: boolean
|
|
23
|
+
is_model: boolean
|
|
24
|
+
is_footprint: boolean
|
|
20
25
|
}): ZT.Package => {
|
|
21
26
|
return {
|
|
22
27
|
...internal_package,
|
|
23
|
-
latest_package_release_id:
|
|
24
|
-
internal_package.latest_package_release_id ?? null,
|
|
28
|
+
latest_package_release_id: internal_package.latest_package_release_id ?? "",
|
|
25
29
|
latest_version: internal_package.latest_version ?? null,
|
|
26
30
|
license: internal_package.latest_license ?? null,
|
|
27
31
|
star_count: internal_package.star_count ?? 0,
|
|
28
32
|
created_at: internal_package.created_at,
|
|
29
33
|
updated_at: internal_package.updated_at,
|
|
34
|
+
is_private: false,
|
|
35
|
+
is_public: true,
|
|
36
|
+
is_unlisted: false,
|
|
30
37
|
}
|
|
31
38
|
}
|