@tscircuit/fake-snippets 0.0.8 → 0.0.10

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 (36) hide show
  1. package/bun-tests/fake-snippets-api/routes/packages/list-1.test.ts +0 -3
  2. package/bun-tests/fake-snippets-api/routes/packages/update.test.ts +161 -0
  3. package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +32 -27
  4. package/bun-tests/fake-snippets-api/routes/snippets/get.test.ts +114 -0
  5. package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +10 -6
  6. package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +8 -6
  7. package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +2 -2
  8. package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +10 -10
  9. package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +8 -6
  10. package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +1 -1
  11. package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +19 -12
  12. package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +57 -16
  13. package/bun.lock +145 -569
  14. package/dist/bundle.js +905 -248
  15. package/fake-snippets-api/lib/db/db-client.ts +748 -108
  16. package/fake-snippets-api/lib/db/schema.ts +12 -0
  17. package/fake-snippets-api/lib/public-mapping/public-map-package.ts +2 -1
  18. package/fake-snippets-api/routes/api/packages/list.ts +4 -1
  19. package/fake-snippets-api/routes/api/packages/update.ts +91 -0
  20. package/fake-snippets-api/routes/api/snippets/add_star.ts +30 -8
  21. package/fake-snippets-api/routes/api/snippets/create.ts +16 -16
  22. package/fake-snippets-api/routes/api/snippets/delete.ts +5 -5
  23. package/fake-snippets-api/routes/api/snippets/download.ts +24 -10
  24. package/fake-snippets-api/routes/api/snippets/get.ts +46 -13
  25. package/fake-snippets-api/routes/api/snippets/list.ts +37 -2
  26. package/fake-snippets-api/routes/api/snippets/update.ts +36 -14
  27. package/package.json +3 -5
  28. package/src/components/CodeAndPreview.tsx +13 -48
  29. package/src/components/CodeEditor.tsx +10 -7
  30. package/src/components/EditorNav.tsx +0 -21
  31. package/src/components/PreviewContent.tsx +2 -2
  32. package/src/components/ViewSnippetHeader.tsx +4 -0
  33. package/src/hooks/use-global-store.ts +0 -5
  34. package/src/hooks/use-package-as-snippet.ts +78 -0
  35. package/src/hooks/use-run-tsx/index.tsx +4 -0
  36. package/src/lib/jlc-parts-engine.ts +4 -2
@@ -91,6 +91,7 @@ export const orderFileSchema = z.object({
91
91
  })
92
92
  export type OrderFile = z.infer<typeof orderFileSchema>
93
93
 
94
+ // TODO: Remove this schema after migration to accountPackages is complete
94
95
  export const accountSnippetSchema = z.object({
95
96
  account_id: z.string(),
96
97
  snippet_id: z.string(),
@@ -100,6 +101,16 @@ export const accountSnippetSchema = z.object({
100
101
  })
101
102
  export type AccountSnippet = z.infer<typeof accountSnippetSchema>
102
103
 
104
+ export const accountPackageSchema = z.object({
105
+ account_package_id: z.string(),
106
+ account_id: z.string(),
107
+ package_id: z.string(),
108
+ is_starred: z.boolean(),
109
+ created_at: z.string(),
110
+ updated_at: z.string(),
111
+ })
112
+ export type AccountPackage = z.infer<typeof accountPackageSchema>
113
+
103
114
  export const packageReleaseSchema = z.object({
104
115
  package_release_id: z.string(),
105
116
  package_id: z.string(),
@@ -172,5 +183,6 @@ export const databaseSchema = z.object({
172
183
  orders: z.array(orderSchema).default([]),
173
184
  orderFiles: z.array(orderFileSchema).default([]),
174
185
  accountSnippets: z.array(accountSnippetSchema).default([]),
186
+ accountPackages: z.array(accountPackageSchema).default([]),
175
187
  })
176
188
  export type DatabaseSchema = z.infer<typeof databaseSchema>
@@ -25,7 +25,8 @@ export const publicMapPackage = (internal_package: {
25
25
  }): ZT.Package => {
26
26
  return {
27
27
  ...internal_package,
28
- latest_package_release_id: internal_package.latest_package_release_id ?? "",
28
+ latest_package_release_id:
29
+ internal_package.latest_package_release_id ?? null,
29
30
  latest_version: internal_package.latest_version ?? null,
30
31
  license: internal_package.latest_license ?? null,
31
32
  star_count: internal_package.star_count ?? 0,
@@ -51,6 +51,9 @@ export default withRouteSpec({
51
51
 
52
52
  return ctx.json({
53
53
  ok: true,
54
- packages,
54
+ packages: packages.map((p) => ({
55
+ ...p,
56
+ latest_package_release_id: p.latest_package_release_id || null,
57
+ })),
55
58
  })
56
59
  })
@@ -0,0 +1,91 @@
1
+ import { z } from "zod"
2
+ import { packageSchema } from "../../../lib/db/schema"
3
+ import { withRouteSpec } from "../../../lib/middleware/with-winter-spec"
4
+
5
+ export default withRouteSpec({
6
+ methods: ["POST"],
7
+ auth: "session",
8
+ jsonBody: z
9
+ .object({
10
+ package_id: z.string(),
11
+ name: z
12
+ .string()
13
+ .regex(
14
+ /^[@a-zA-Z0-9-_\/]+$/,
15
+ "Package name can only contain letters, numbers, hyphens, underscores, and forward slashes",
16
+ )
17
+ .transform((name) => name.replace(/^@/, ""))
18
+ .optional(),
19
+ description: z.string().optional(),
20
+ is_private: z.boolean().optional(),
21
+ is_unlisted: z.boolean().optional(),
22
+ })
23
+ .transform((data) => ({
24
+ ...data,
25
+ is_unlisted: data.is_private ? true : data.is_unlisted,
26
+ })),
27
+ jsonResponse: z.object({
28
+ ok: z.boolean(),
29
+ package: packageSchema,
30
+ }),
31
+ })(async (req, ctx) => {
32
+ const { package_id, name, description, is_private, is_unlisted } =
33
+ req.jsonBody
34
+
35
+ const packageIndex = ctx.db.packages.findIndex(
36
+ (p) => p.package_id === package_id,
37
+ )
38
+
39
+ if (packageIndex === -1) {
40
+ return ctx.error(404, {
41
+ error_code: "package_not_found",
42
+ message: "Package not found",
43
+ })
44
+ }
45
+
46
+ const existingPackage = ctx.db.packages[packageIndex]
47
+
48
+ // Check if user has permission to update the package
49
+ if (existingPackage.creator_account_id !== ctx.auth.account_id) {
50
+ return ctx.error(403, {
51
+ error_code: "forbidden",
52
+ message: "You don't have permission to update this package",
53
+ })
54
+ }
55
+
56
+ // Check for duplicate package name if name is being updated
57
+ if (name) {
58
+ const newFullName = `${ctx.auth.github_username}/${name}`
59
+ const duplicatePackage = ctx.db.packages.find(
60
+ (p) => p.name === newFullName && p.package_id !== package_id,
61
+ )
62
+ if (duplicatePackage) {
63
+ return ctx.error(400, {
64
+ error_code: "package_already_exists",
65
+ message: "A package with this name already exists",
66
+ })
67
+ }
68
+ }
69
+
70
+ const updatedPackage = ctx.db.updatePackage(package_id, {
71
+ name: name ? `${ctx.auth.github_username}/${name}` : existingPackage.name,
72
+ description: description ?? existingPackage.description,
73
+ is_private: is_private ?? existingPackage.is_private,
74
+ is_public:
75
+ is_private !== undefined ? !is_private : existingPackage.is_public,
76
+ is_unlisted: is_unlisted ?? existingPackage.is_unlisted,
77
+ updated_at: new Date().toISOString(),
78
+ })
79
+
80
+ if (!updatedPackage) {
81
+ return ctx.error(500, {
82
+ error_code: "update_failed",
83
+ message: "Failed to update package",
84
+ })
85
+ }
86
+
87
+ return ctx.json({
88
+ ok: true,
89
+ package: updatedPackage,
90
+ })
91
+ })
@@ -1,6 +1,5 @@
1
1
  import { withRouteSpec } from "fake-snippets-api/lib/middleware/with-winter-spec"
2
2
  import { z } from "zod"
3
- import { accountSnippetSchema } from "fake-snippets-api/lib/db/schema"
4
3
 
5
4
  export default withRouteSpec({
6
5
  methods: ["POST"],
@@ -10,13 +9,15 @@ export default withRouteSpec({
10
9
  }),
11
10
  jsonResponse: z.object({
12
11
  ok: z.boolean(),
13
- account_snippet: accountSnippetSchema,
14
12
  }),
15
13
  })(async (req, ctx) => {
16
14
  const { snippet_id } = req.jsonBody
17
15
 
18
- // Check if snippet exists
19
- const snippet = ctx.db.getSnippetById(snippet_id)
16
+ // Check if snippet exists (as a package)
17
+ const snippet = ctx.db.packages.find(
18
+ (pkg) => pkg.package_id === snippet_id && pkg.is_snippet === true,
19
+ )
20
+
20
21
  if (!snippet) {
21
22
  return ctx.error(404, {
22
23
  error_code: "snippet_not_found",
@@ -25,18 +26,39 @@ export default withRouteSpec({
25
26
  }
26
27
 
27
28
  // Check if already starred
28
- if (ctx.db.hasStarred(ctx.auth.account_id, snippet_id)) {
29
+ const existing = ctx.db.accountPackages.find(
30
+ (ap) =>
31
+ ap.account_id === ctx.auth.account_id && ap.package_id === snippet_id,
32
+ )
33
+
34
+ if (existing?.is_starred) {
29
35
  return ctx.error(400, {
30
36
  error_code: "already_starred",
31
37
  message: "You have already starred this snippet",
32
38
  })
33
39
  }
34
40
 
35
- // Add star
36
- const accountSnippet = ctx.db.addStar(ctx.auth.account_id, snippet_id)
41
+ // Update the package's star count
42
+ snippet.star_count = (snippet.star_count || 0) + 1
43
+
44
+ if (existing) {
45
+ // If record exists but is_starred is false, update to is_starred=true
46
+ existing.is_starred = true
47
+ existing.updated_at = new Date().toISOString()
48
+ } else {
49
+ // Add star by creating a new account_package record
50
+ const newAccountPackage = {
51
+ account_package_id: `ap_${Math.random().toString(36).substring(2, 15)}`,
52
+ account_id: ctx.auth.account_id,
53
+ package_id: snippet_id,
54
+ is_starred: true,
55
+ created_at: new Date().toISOString(),
56
+ updated_at: new Date().toISOString(),
57
+ }
58
+ ctx.db.addAccountPackage(newAccountPackage)
59
+ }
37
60
 
38
61
  return ctx.json({
39
62
  ok: true,
40
- account_snippet: accountSnippet,
41
63
  })
42
64
  })
@@ -29,6 +29,9 @@ export default withRouteSpec({
29
29
  dts,
30
30
  } = req.jsonBody
31
31
 
32
+ const timestamp = Date.now()
33
+ const currentTime = new Date(timestamp).toISOString()
34
+
32
35
  if (!unscoped_name) {
33
36
  // Count snippets of this type for this user
34
37
  const userSnippets = ctx.db.packages.filter(
@@ -59,7 +62,7 @@ export default withRouteSpec({
59
62
  try {
60
63
  // Create the package directly (which will serve as our snippet)
61
64
  const newPackage = {
62
- package_id: `pkg_${Date.now()}`,
65
+ package_id: `pkg_${timestamp}`,
63
66
  creator_account_id: ctx.auth.account_id,
64
67
  owner_org_id: ctx.auth.personal_org_id,
65
68
  owner_github_username: ctx.auth.github_username,
@@ -70,8 +73,8 @@ export default withRouteSpec({
70
73
  latest_version: "0.0.1",
71
74
  license: null,
72
75
  star_count: 0,
73
- created_at: new Date().toISOString(),
74
- updated_at: new Date().toISOString(),
76
+ created_at: currentTime,
77
+ updated_at: currentTime,
75
78
  ai_description: null,
76
79
  is_snippet: true,
77
80
  is_board: snippet_type === "board",
@@ -82,32 +85,29 @@ export default withRouteSpec({
82
85
  is_private: false,
83
86
  is_public: true,
84
87
  is_unlisted: false,
85
- latest_package_release_id: "",
88
+ latest_package_release_id: `package_release_${timestamp}`,
86
89
  }
87
90
 
88
91
  ctx.db.addPackage(newPackage)
89
92
 
90
93
  const newPackageRelease = {
91
- package_release_id: `package_release_${Date.now()}`,
94
+ package_release_id: `package_release_${timestamp}`,
92
95
  package_id: newPackage.package_id,
93
96
  version: "0.0.1",
94
97
  is_latest: true,
95
98
  is_locked: false,
96
- created_at: newPackage.created_at,
99
+ created_at: currentTime,
97
100
  }
98
101
 
99
102
  ctx.db.addPackageRelease(newPackageRelease)
100
103
 
101
- // Update the package with the release ID
102
- newPackage.latest_package_release_id = newPackageRelease.package_release_id
103
-
104
104
  // Add package files
105
105
  // Add index.tsx file with the code content
106
106
  ctx.db.addPackageFile({
107
107
  package_release_id: newPackageRelease.package_release_id,
108
108
  file_path: "index.tsx",
109
109
  content_text: code,
110
- created_at: new Date().toISOString(),
110
+ created_at: currentTime,
111
111
  })
112
112
 
113
113
  // Add DTS file if provided
@@ -116,7 +116,7 @@ export default withRouteSpec({
116
116
  package_release_id: newPackageRelease.package_release_id,
117
117
  file_path: "/dist/index.d.ts",
118
118
  content_text: dts,
119
- created_at: new Date().toISOString(),
119
+ created_at: currentTime,
120
120
  })
121
121
  }
122
122
 
@@ -126,7 +126,7 @@ export default withRouteSpec({
126
126
  package_release_id: newPackageRelease.package_release_id,
127
127
  file_path: "/dist/index.js",
128
128
  content_text: compiled_js,
129
- created_at: new Date().toISOString(),
129
+ created_at: currentTime,
130
130
  })
131
131
  }
132
132
 
@@ -136,7 +136,7 @@ export default withRouteSpec({
136
136
  package_release_id: newPackageRelease.package_release_id,
137
137
  file_path: "/dist/circuit.json",
138
138
  content_text: JSON.stringify(circuit_json),
139
- created_at: new Date().toISOString(),
139
+ created_at: currentTime,
140
140
  })
141
141
  }
142
142
 
@@ -151,10 +151,10 @@ export default withRouteSpec({
151
151
  dts,
152
152
  compiled_js,
153
153
  star_count: 0,
154
- created_at: newPackage.created_at,
155
- updated_at: newPackage.updated_at,
154
+ created_at: currentTime,
155
+ updated_at: currentTime,
156
156
  snippet_type: snippet_type,
157
- circuit_json: circuit_json,
157
+ circuit_json: circuit_json || [],
158
158
  description: description,
159
159
  is_starred: false,
160
160
  version: "0.0.1",
@@ -13,8 +13,8 @@ export default withRouteSpec({
13
13
  })(async (req, ctx) => {
14
14
  const { snippet_id } = req.jsonBody
15
15
 
16
- const snippetIndex = ctx.db.snippets.findIndex(
17
- (s) => s.snippet_id === snippet_id,
16
+ const snippetIndex = ctx.db.packages.findIndex(
17
+ (s) => s.package_id === snippet_id,
18
18
  )
19
19
 
20
20
  if (snippetIndex === -1) {
@@ -24,16 +24,16 @@ export default withRouteSpec({
24
24
  })
25
25
  }
26
26
 
27
- const snippet = ctx.db.snippets[snippetIndex]
27
+ const snippet = ctx.db.packages[snippetIndex]
28
28
 
29
- if (snippet.owner_name !== ctx.auth.github_username) {
29
+ if (snippet.creator_account_id !== ctx.auth.github_username) {
30
30
  return ctx.error(403, {
31
31
  error_code: "forbidden",
32
32
  message: "You don't have permission to delete this snippet",
33
33
  })
34
34
  }
35
35
 
36
- ctx.db.snippets.splice(snippetIndex, 1)
36
+ ctx.db.packages.splice(snippetIndex, 1)
37
37
 
38
38
  return ctx.json({
39
39
  ok: true,
@@ -21,11 +21,19 @@ export default withRouteSpec({
21
21
  const fileName = rest.join("/")
22
22
 
23
23
  // Find the snippet
24
- const snippet = ctx.db.snippets.find(
25
- (s) => s.owner_name === owner && s.unscoped_name === packageName,
24
+ const _package = ctx.db.packages.find(
25
+ (s) => s.owner_github_username === owner && s.unscoped_name === packageName,
26
26
  )
27
27
 
28
- if (!snippet) {
28
+ const packageRelease = ctx.db.packageReleases.find(
29
+ (p) => p.package_id === _package?.package_id,
30
+ )
31
+
32
+ const packageFiles = ctx.db.packageFiles.filter(
33
+ (p) => p.package_release_id === packageRelease?.package_release_id,
34
+ )
35
+
36
+ if (!_package) {
29
37
  return ctx.error(404, {
30
38
  error_code: "snippet_not_found",
31
39
  message: "Snippet not found",
@@ -64,21 +72,23 @@ export default withRouteSpec({
64
72
  type: "file",
65
73
  name: "index.ts",
66
74
  hash: "placeholder_hash",
67
- time: snippet.updated_at,
68
- size: snippet.code.length,
75
+ time: packageRelease?.created_at,
76
+ size: packageFiles.find((f) => f.file_path === "index.ts")?.content_text
77
+ ?.length,
69
78
  },
70
79
  {
71
80
  type: "file",
72
81
  name: "index.d.ts",
73
82
  hash: "placeholder_hash",
74
- time: snippet.updated_at,
75
- size: (snippet.dts || "").length,
83
+ time: packageRelease?.created_at,
84
+ size: packageFiles.find((f) => f.file_path === "index.d.ts")
85
+ ?.content_text?.length,
76
86
  },
77
87
  {
78
88
  type: "file",
79
89
  name: "package.json",
80
90
  hash: "placeholder_hash",
81
- time: snippet.updated_at,
91
+ time: packageRelease?.created_at,
82
92
  size: JSON.stringify({
83
93
  name: `@tsci/${owner}.${packageName}`,
84
94
  version: version || "0.0.1",
@@ -117,10 +127,14 @@ export default withRouteSpec({
117
127
  let content: string
118
128
  switch (fileName) {
119
129
  case "index.ts":
120
- content = snippet.code
130
+ content =
131
+ packageFiles.find((f) => f.file_path === "index.tsx")?.content_text ||
132
+ ""
121
133
  break
122
134
  case "index.d.ts":
123
- content = snippet.dts || ""
135
+ content =
136
+ packageFiles.find((f) => f.file_path === "/dist/index.d.ts")
137
+ ?.content_text || ""
124
138
  break
125
139
  case "package.json":
126
140
  content = JSON.stringify(
@@ -1,6 +1,6 @@
1
+ import { snippetSchema } from "fake-snippets-api/lib/db/schema"
1
2
  import { withRouteSpec } from "fake-snippets-api/lib/middleware/with-winter-spec"
2
3
  import { z } from "zod"
3
- import { snippetSchema } from "fake-snippets-api/lib/db/schema"
4
4
 
5
5
  export default withRouteSpec({
6
6
  methods: ["GET", "POST"],
@@ -19,16 +19,50 @@ export default withRouteSpec({
19
19
  })(async (req, ctx) => {
20
20
  const { snippet_id, name, owner_name, unscoped_name } = req.commonParams
21
21
 
22
- const foundSnippet =
23
- (snippet_id && ctx.db.getSnippetById(snippet_id)) ||
24
- ctx.db.snippets.find((s) => {
25
- if (name && s.name !== name) return false
26
- if (owner_name && s.owner_name !== owner_name) return false
27
- if (unscoped_name && s.unscoped_name !== unscoped_name) return false
28
- return true
22
+ // First try to find by snippet_id
23
+ if (snippet_id) {
24
+ const foundSnippet = ctx.db.getSnippetById(snippet_id)
25
+ if (foundSnippet) {
26
+ if (ctx.auth) {
27
+ foundSnippet.is_starred = ctx.db.hasStarred(
28
+ ctx.auth.account_id,
29
+ foundSnippet.snippet_id,
30
+ )
31
+ }
32
+ return ctx.json({
33
+ ok: true,
34
+ snippet: foundSnippet,
35
+ })
36
+ }
37
+ }
38
+
39
+ // If not found by ID, try to find by other parameters
40
+ const foundPackage = ctx.db.packages.find((pkg) => {
41
+ if (!pkg.is_snippet) return false
42
+ if (name && pkg.name.toLowerCase() !== name.toLowerCase()) return false
43
+ if (
44
+ owner_name &&
45
+ pkg.owner_github_username?.toLowerCase() !== owner_name.toLowerCase()
46
+ )
47
+ return false
48
+ if (
49
+ unscoped_name &&
50
+ pkg.unscoped_name.toLowerCase() !== unscoped_name.toLowerCase()
51
+ )
52
+ return false
53
+ return true
54
+ })
55
+
56
+ if (!foundPackage) {
57
+ return ctx.error(404, {
58
+ error_code: "snippet_not_found",
59
+ message: `Snippet not found (searched using ${JSON.stringify(req.commonParams)})`,
29
60
  })
61
+ }
30
62
 
31
- if (!foundSnippet) {
63
+ // Convert package to snippet format
64
+ const snippet = ctx.db.getSnippetById(foundPackage.package_id)
65
+ if (!snippet) {
32
66
  return ctx.error(404, {
33
67
  error_code: "snippet_not_found",
34
68
  message: `Snippet not found (searched using ${JSON.stringify(req.commonParams)})`,
@@ -36,15 +70,14 @@ export default withRouteSpec({
36
70
  }
37
71
 
38
72
  if (ctx.auth) {
39
- const starred = ctx.db.hasStarred(
73
+ snippet.is_starred = ctx.db.hasStarred(
40
74
  ctx.auth.account_id,
41
- foundSnippet.snippet_id,
75
+ snippet.snippet_id,
42
76
  )
43
- foundSnippet.is_starred = starred
44
77
  }
45
78
 
46
79
  return ctx.json({
47
80
  ok: true,
48
- snippet: foundSnippet,
81
+ snippet,
49
82
  })
50
83
  })
@@ -17,10 +17,45 @@ export default withRouteSpec({
17
17
  })(async (req, ctx) => {
18
18
  const { owner_name, unscoped_name } = req.commonParams
19
19
 
20
- const snippets = ctx.db
21
- .getSnippetsByAuthor(owner_name)
20
+ const packages = ctx.db
21
+ .getPackagesByAuthor(owner_name)
22
22
  .filter((s) => !unscoped_name || s.unscoped_name === unscoped_name)
23
23
 
24
+ const snippets = packages.map((pkg) => {
25
+ const packageRelease = ctx.db.getPackageReleaseById(
26
+ pkg.latest_package_release_id || "",
27
+ )
28
+ const packageFiles = ctx.db.getPackageFilesByReleaseId(
29
+ packageRelease?.package_release_id || "",
30
+ )
31
+ const codeFile = packageFiles.find(
32
+ (file) => file.file_path === "index.ts" || file.file_path === "index.tsx",
33
+ )
34
+ const starCount = ctx.db.getStarCount(pkg.package_id)
35
+
36
+ return {
37
+ snippet_id: pkg.package_id,
38
+ package_release_id: pkg.latest_package_release_id || "",
39
+ unscoped_name: pkg.unscoped_name,
40
+ name: pkg.name,
41
+ owner_name: pkg.owner_github_username || "",
42
+ description: pkg.description || "",
43
+ snippet_type: pkg.snippet_type || "board",
44
+ code: codeFile?.content_text || "",
45
+ dts:
46
+ packageFiles.find((file) => file.file_path === "/dist/index.d.ts")
47
+ ?.content_text || "",
48
+ compiled_js:
49
+ packageFiles.find((file) => file.file_path === "/dist/index.js")
50
+ ?.content_text || "",
51
+ created_at: pkg.created_at,
52
+ updated_at: pkg.updated_at,
53
+ star_count: starCount,
54
+ is_starred: false,
55
+ version: pkg.latest_version || "0.0.1",
56
+ }
57
+ })
58
+
24
59
  return ctx.json({
25
60
  ok: true,
26
61
  snippets,
@@ -33,20 +33,39 @@ export default withRouteSpec({
33
33
  manual_edits_json_content,
34
34
  } = req.jsonBody
35
35
 
36
- const snippetIndex = ctx.db.snippets.findIndex(
37
- (s) => s.snippet_id === snippet_id,
36
+ const packageIndex = ctx.db.packages.findIndex(
37
+ (s) => s.package_id === snippet_id,
38
38
  )
39
39
 
40
- if (snippetIndex === -1) {
40
+ if (packageIndex === -1) {
41
41
  return ctx.error(404, {
42
42
  error_code: "snippet_not_found",
43
43
  message: "Snippet not found",
44
44
  })
45
45
  }
46
46
 
47
- const snippet = ctx.db.snippets[snippetIndex]
47
+ const _package = ctx.db.packages[packageIndex]
48
+ const packageRelease = ctx.db.packageReleases.find(
49
+ (r) => r.package_release_id === _package.latest_package_release_id,
50
+ )
51
+ const packageFiles = ctx.db.packageFiles.filter(
52
+ (f) => f.package_release_id === packageRelease?.package_release_id,
53
+ )
54
+ const codeFile = packageFiles.find(
55
+ (f) => f.file_path === "index.tsx" || f.file_path === "index.ts",
56
+ )
57
+ const dtsFile = packageFiles.find((f) => f.file_path === "/dist/index.d.ts")
58
+ const compiledJsFile = packageFiles.find(
59
+ (f) => f.file_path === "/dist/index.js",
60
+ )
61
+ const manualEditsJsonFile = packageFiles.find(
62
+ (f) => f.file_path === "manual-edits.json",
63
+ )
64
+ const circuitJsonFile = packageFiles.find(
65
+ (f) => f.file_path === "/dist/circuit.json",
66
+ )
48
67
 
49
- if (snippet.owner_name !== ctx.auth.github_username) {
68
+ if (_package.creator_account_id !== ctx.auth.account_id) {
50
69
  return ctx.error(403, {
51
70
  error_code: "forbidden",
52
71
  message: "You don't have permission to update this snippet",
@@ -54,21 +73,24 @@ export default withRouteSpec({
54
73
  }
55
74
 
56
75
  const updatedSnippet = ctx.db.updateSnippet(snippet_id, {
57
- code: code ?? snippet.code,
58
- description: description ?? snippet.description,
59
- unscoped_name: unscoped_name ?? snippet.unscoped_name,
76
+ code: code ?? codeFile?.content_text ?? "",
77
+ description: description ?? _package.description ?? "",
78
+ unscoped_name: unscoped_name ?? _package.unscoped_name,
60
79
  name: unscoped_name
61
80
  ? `${ctx.auth.github_username}/${unscoped_name}`
62
- : snippet.name,
63
- dts: dts ?? snippet.dts,
64
- compiled_js: compiled_js !== undefined ? compiled_js : snippet.compiled_js,
81
+ : _package.name,
82
+ dts: dts ?? dtsFile?.content_text ?? "",
83
+ compiled_js: compiled_js ?? compiledJsFile?.content_text ?? "",
65
84
  manual_edits_json_content:
66
85
  manual_edits_json_content !== undefined
67
86
  ? manual_edits_json_content
68
- : snippet.manual_edits_json_content,
87
+ : (manualEditsJsonFile?.content_text ?? ""),
69
88
  circuit_json:
70
- circuit_json !== undefined ? circuit_json : snippet.circuit_json,
71
- snippet_type: snippet_type ?? snippet.snippet_type,
89
+ circuit_json ??
90
+ (circuitJsonFile?.content_text
91
+ ? JSON.parse(circuitJsonFile.content_text)
92
+ : []),
93
+ snippet_type: snippet_type ?? _package.snippet_type,
72
94
  updated_at: new Date().toISOString(),
73
95
  })
74
96
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscircuit/fake-snippets",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -66,9 +66,7 @@
66
66
  "@tscircuit/layout": "^0.0.29",
67
67
  "@tscircuit/math-utils": "^0.0.10",
68
68
  "@tscircuit/mm": "^0.0.8",
69
- "@tscircuit/pcb-viewer": "^1.11.12",
70
69
  "@tscircuit/props": "^0.0.143",
71
- "@tscircuit/schematic-viewer": "^1.4.3",
72
70
  "@types/file-saver": "^2.0.7",
73
71
  "@types/ms": "^0.7.34",
74
72
  "@typescript/ata": "^0.9.7",
@@ -130,9 +128,9 @@
130
128
  "@babel/standalone": "^7.26.2",
131
129
  "@biomejs/biome": "^1.9.2",
132
130
  "@playwright/test": "^1.48.0",
133
- "@tscircuit/core": "^0.0.315",
131
+ "@tscircuit/core": "^0.0.345",
134
132
  "@tscircuit/prompt-benchmarks": "^0.0.28",
135
- "@tscircuit/runframe": "^0.0.193",
133
+ "@tscircuit/runframe": "^0.0.231",
136
134
  "@types/babel__standalone": "^7.1.7",
137
135
  "@types/bun": "^1.1.10",
138
136
  "@types/country-list": "^2.1.4",