nowebcli 0.2.8 → 0.2.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nowebcli",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "Create, preview and publish community templates for Noweb",
5
5
  "license": "MIT",
6
6
  "repository": {
package/scripts/noweb.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { spawn, spawnSync } from "node:child_process";
4
- import { createHash } from "node:crypto";
4
+ import { createHash, randomBytes } from "node:crypto";
5
5
  import { existsSync } from "node:fs";
6
6
  import { createServer } from "node:http";
7
7
  import { access, chmod, mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
@@ -42,7 +42,7 @@ function usage() {
42
42
  }
43
43
 
44
44
  async function createTemplate(value) {
45
- const validSlug = requireSlug(value || basename(root));
45
+ const validSlug = requireSlug(value || `template-${randomBytes(6).toString("hex")}`);
46
46
  const directory = root;
47
47
 
48
48
  for (const file of ["Template.tsx", "customizations.json", "template.json"]) {
@@ -110,42 +110,27 @@ export type ProjectDataRecord<T extends Record<string, unknown> = Record<string,
110
110
  data: T;
111
111
  };
112
112
 
113
- class ProjectDataRequestError extends Error {
114
- constructor(message: string, readonly status: number) {
115
- super(message);
116
- }
117
- }
118
-
119
113
  export const db = {
120
- async isAdmin(adminName: string) {
121
- try {
122
- await projectDataRequest("?admin=1", { adminName });
123
- return true;
124
- } catch (error) {
125
- if (error instanceof ProjectDataRequestError && error.status === 401) return false;
126
- throw error;
127
- }
128
- },
129
- list<T extends Record<string, unknown> = Record<string, unknown>>(adminName?: string) {
130
- return projectDataRequest<ProjectDataRecord<T>[]>(adminName ? "?admin=1" : "", { adminName });
114
+ list<T extends Record<string, unknown> = Record<string, unknown>>() {
115
+ return projectDataRequest<ProjectDataRecord<T>[]>("");
131
116
  },
132
- create<T extends Record<string, unknown>>(data: T, adminName: string) {
133
- return projectDataRequest<ProjectDataRecord<T>>("", { adminName, body: data, method: "POST" });
117
+ create<T extends Record<string, unknown>>(data: T) {
118
+ return projectDataRequest<ProjectDataRecord<T>>("", { body: data, method: "POST" });
134
119
  },
135
- update<T extends Record<string, unknown>>(id: string | number, data: T, adminName: string) {
136
- return projectDataRequest<ProjectDataRecord<T>>(`?id=${id}`, { adminName, body: data, method: "PATCH" });
120
+ update<T extends Record<string, unknown>>(id: string | number, data: T) {
121
+ return projectDataRequest<ProjectDataRecord<T>>(`?id=${id}`, { body: data, method: "PATCH" });
137
122
  },
138
- remove(id: string | number, adminName: string) {
139
- return projectDataRequest<void>(`?id=${id}`, { adminName, method: "DELETE" });
123
+ remove(id: string | number) {
124
+ return projectDataRequest<void>(`?id=${id}`, { method: "DELETE" });
140
125
  },
141
126
  claim<T extends Record<string, unknown>>(id: string | number, data: T) {
142
- return projectDataRequest<ProjectDataRecord<T>>(`?id=${id}`, { body: { claim: data }, method: "PATCH" });
127
+ return projectDataRequest<ProjectDataRecord<T>>(`?id=${id}&claim=1`, { body: data, method: "PATCH" });
143
128
  },
144
129
  };
145
130
 
146
131
  async function projectDataRequest<T>(
147
132
  path: string,
148
- options: { adminName?: string; body?: Record<string, unknown>; method?: string },
133
+ options: { body?: Record<string, unknown>; method?: string } = {},
149
134
  ) {
150
135
  const { dataApiBase, projectId } = currentTemplateRuntime;
151
136
  if (!projectId) throw new Error("Database access is available inside projects only");
@@ -156,14 +141,13 @@ async function projectDataRequest<T>(
156
141
  method: options.method || "GET",
157
142
  headers: {
158
143
  ...(options.body ? { "Content-Type": "application/json" } : {}),
159
- ...(options.adminName ? { "X-Admin-Name": options.adminName } : {}),
160
144
  },
161
145
  body: options.body ? JSON.stringify(options.body) : undefined,
162
146
  },
163
147
  );
164
148
  const result = response.status === 204 ? null : await response.json().catch(() => null);
165
149
  if (!response.ok) {
166
- throw new ProjectDataRequestError(result?.error || `Database request failed (${response.status})`, response.status);
150
+ throw new Error(result?.error || `Database request failed (${response.status})`);
167
151
  }
168
152
  return result as T;
169
153
  }