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 +1 -1
- package/scripts/noweb.mjs +2 -2
- package/templates/templateRuntime.tsx +11 -27
package/package.json
CHANGED
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 ||
|
|
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
|
-
|
|
121
|
-
|
|
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
|
|
133
|
-
return projectDataRequest<ProjectDataRecord<T>>("", {
|
|
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
|
|
136
|
-
return projectDataRequest<ProjectDataRecord<T>>(`?id=${id}`, {
|
|
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
|
|
139
|
-
return projectDataRequest<void>(`?id=${id}`, {
|
|
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:
|
|
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: {
|
|
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
|
|
150
|
+
throw new Error(result?.error || `Database request failed (${response.status})`);
|
|
167
151
|
}
|
|
168
152
|
return result as T;
|
|
169
153
|
}
|