@77-zhike/app-sdk 0.2.0-d7b1.3 → 0.2.0-d7b1.4
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/src/cli/project-init.js +1 -170
package/package.json
CHANGED
package/src/cli/project-init.js
CHANGED
|
@@ -95,7 +95,7 @@ export async function scaffoldProject({ targetDirectory, binding }) {
|
|
|
95
95
|
try {
|
|
96
96
|
await mkdir(resolve(temporaryDirectory, ".zhike"), { recursive: true });
|
|
97
97
|
await mkdir(
|
|
98
|
-
resolve(temporaryDirectory, "src", "app", "extensions"
|
|
98
|
+
resolve(temporaryDirectory, "src", "app", "extensions"),
|
|
99
99
|
{ recursive: true },
|
|
100
100
|
);
|
|
101
101
|
await mkdir(resolve(temporaryDirectory, "src", "app", "webhooks"), {
|
|
@@ -136,175 +136,6 @@ export async function scaffoldProject({ targetDirectory, binding }) {
|
|
|
136
136
|
)}\n`,
|
|
137
137
|
{ flag: "wx" },
|
|
138
138
|
);
|
|
139
|
-
await writeFile(
|
|
140
|
-
resolve(
|
|
141
|
-
temporaryDirectory,
|
|
142
|
-
"src",
|
|
143
|
-
"app",
|
|
144
|
-
"events",
|
|
145
|
-
"app-installed.event.ts",
|
|
146
|
-
),
|
|
147
|
-
`import { createWebhookHandler, ZHIKE_API_TOKEN, kv } from "@77-zhike/app-sdk/server";
|
|
148
|
-
|
|
149
|
-
export default async function appInstalled() {
|
|
150
|
-
const handler = await createWebhookHandler({ fileName: "qiqi-customer" });
|
|
151
|
-
const existing = await fetch("https://api-clm.77hub.com/v1/webhooks", {
|
|
152
|
-
headers: { authorization: \`Bearer \${ZHIKE_API_TOKEN}\` },
|
|
153
|
-
});
|
|
154
|
-
if (!existing.ok) throw new Error("webhook_list_failed");
|
|
155
|
-
const { items } = await existing.json() as { items: Array<{ subscriptions: Array<{ eventType: string }> }> };
|
|
156
|
-
const requiredEventTypes = ["record.created", "activity.updated"];
|
|
157
|
-
if (items.some((item) => requiredEventTypes.every((eventType) =>
|
|
158
|
-
item.subscriptions.some((subscription) => subscription.eventType === eventType)
|
|
159
|
-
))) return;
|
|
160
|
-
|
|
161
|
-
const response = await fetch("https://api-clm.77hub.com/v1/webhooks", {
|
|
162
|
-
method: "POST",
|
|
163
|
-
headers: {
|
|
164
|
-
authorization: \`Bearer \${ZHIKE_API_TOKEN}\`,
|
|
165
|
-
"content-type": "application/json",
|
|
166
|
-
},
|
|
167
|
-
body: JSON.stringify({
|
|
168
|
-
targetUrl: handler.url,
|
|
169
|
-
subscriptions: [{
|
|
170
|
-
eventType: "record.created",
|
|
171
|
-
filter: { field: "id.objectSlug", operator: "equals", value: "accounts" },
|
|
172
|
-
}, {
|
|
173
|
-
eventType: "activity.updated",
|
|
174
|
-
filter: null,
|
|
175
|
-
}],
|
|
176
|
-
}),
|
|
177
|
-
});
|
|
178
|
-
if (!response.ok) throw new Error("webhook_create_failed");
|
|
179
|
-
const created = await response.json() as { webhook: { id: string }; secret: string };
|
|
180
|
-
await kv.set(
|
|
181
|
-
\`webhook:\${created.webhook.id}\`,
|
|
182
|
-
JSON.stringify({ webhookId: created.webhook.id, secret: created.secret }),
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
`,
|
|
186
|
-
{ flag: "wx" },
|
|
187
|
-
);
|
|
188
|
-
await writeFile(
|
|
189
|
-
resolve(
|
|
190
|
-
temporaryDirectory,
|
|
191
|
-
"src",
|
|
192
|
-
"app",
|
|
193
|
-
"events",
|
|
194
|
-
"app-uninstalled.event.ts",
|
|
195
|
-
),
|
|
196
|
-
`import { deleteWebhookHandler, listWebhookHandlers, ZHIKE_API_TOKEN, kv } from "@77-zhike/app-sdk/server";\n\nexport default async function appUninstalled() {\n const response = await fetch("https://api-clm.77hub.com/v1/webhooks", {\n headers: { authorization: \`Bearer \${ZHIKE_API_TOKEN}\` },\n });\n if (!response.ok) throw new Error("webhook_list_failed");\n const { items } = await response.json() as { items: Array<{ id: string }> };\n for (const webhook of items) {\n const deleted = await fetch(\`https://api-clm.77hub.com/v1/webhooks/\${webhook.id}\`, {\n method: "DELETE",\n headers: { authorization: \`Bearer \${ZHIKE_API_TOKEN}\` },\n });\n if (deleted.status !== 204 && deleted.status !== 404) throw new Error("webhook_delete_failed");\n await kv.delete(\`webhook:\${webhook.id}\`);\n }\n for (const handler of await listWebhookHandlers()) {\n await deleteWebhookHandler(handler.id);\n }\n}\n`,
|
|
197
|
-
{ flag: "wx" },
|
|
198
|
-
);
|
|
199
|
-
await writeFile(
|
|
200
|
-
resolve(
|
|
201
|
-
temporaryDirectory,
|
|
202
|
-
"src",
|
|
203
|
-
"app",
|
|
204
|
-
"events",
|
|
205
|
-
"connection-added.event.ts",
|
|
206
|
-
),
|
|
207
|
-
`import { kv } from "@77-zhike/app-sdk/server";\nimport type { ConnectionAddedParams } from "@77-zhike/app-sdk/server";\n\nexport default async function connectionAdded({ connection }: ConnectionAddedParams) {\n const stateKey = \`connection:\${connection.id}:\${connection.generation}:initialized\`;\n if (await kv.get(stateKey)) return;\n // 在这里调用外部 OpenAPI 创建订阅,再持久化供应商返回的引用。\n await kv.set(stateKey, "initialized");\n}\n`,
|
|
208
|
-
{ flag: "wx" },
|
|
209
|
-
);
|
|
210
|
-
await writeFile(
|
|
211
|
-
resolve(
|
|
212
|
-
temporaryDirectory,
|
|
213
|
-
"src",
|
|
214
|
-
"app",
|
|
215
|
-
"events",
|
|
216
|
-
"connection-removed.event.ts",
|
|
217
|
-
),
|
|
218
|
-
`import { kv } from "@77-zhike/app-sdk/server";\nimport type { ConnectionRemovedParams } from "@77-zhike/app-sdk/server";\n\nexport default async function connectionRemoved({ connection }: ConnectionRemovedParams) {\n const stateKey = \`connection:\${connection.id}:\${connection.generation}:initialized\`;\n // 在这里读取供应商引用并调用外部 OpenAPI 删除订阅。\n await kv.delete(stateKey);\n}\n`,
|
|
219
|
-
{ flag: "wx" },
|
|
220
|
-
);
|
|
221
|
-
await writeFile(
|
|
222
|
-
resolve(
|
|
223
|
-
temporaryDirectory,
|
|
224
|
-
"src",
|
|
225
|
-
"app",
|
|
226
|
-
"extensions",
|
|
227
|
-
"customer-action",
|
|
228
|
-
"extension.tsx",
|
|
229
|
-
),
|
|
230
|
-
`import { defineFrontendExtension } from "@77-zhike/app-sdk/client";\nimport createFollowUpTask from "../../../server/functions/create-follow-up-task.server";\n\nexport default defineFrontendExtension({\n key: "customer-action",\n type: "record-action",\n label: "创建跟进任务",\n icon: "refresh-cw",\n objects: "accounts",\n async onTrigger({ recordId }: { recordId: string }) {\n await createFollowUpTask({ customerId: recordId });\n },\n});\n`,
|
|
231
|
-
{ flag: "wx" },
|
|
232
|
-
);
|
|
233
|
-
await writeFile(
|
|
234
|
-
resolve(
|
|
235
|
-
temporaryDirectory,
|
|
236
|
-
"src",
|
|
237
|
-
"server",
|
|
238
|
-
"functions",
|
|
239
|
-
"create-follow-up-task.server.ts",
|
|
240
|
-
),
|
|
241
|
-
`import { ZHIKE_API_TOKEN } from "@77-zhike/app-sdk/server";
|
|
242
|
-
|
|
243
|
-
export default async function(input: { customerId: string }) {
|
|
244
|
-
const response = await fetch("https://api-clm.77hub.com/v1/tasks", {
|
|
245
|
-
method: "POST",
|
|
246
|
-
headers: {
|
|
247
|
-
authorization: \`Bearer \${ZHIKE_API_TOKEN}\`,
|
|
248
|
-
"content-type": "application/json",
|
|
249
|
-
},
|
|
250
|
-
body: JSON.stringify({
|
|
251
|
-
title: "跟进客户",
|
|
252
|
-
relatedRecords: [{ objectSlug: "accounts", recordId: input.customerId }],
|
|
253
|
-
}),
|
|
254
|
-
});
|
|
255
|
-
if (!response.ok) throw new Error("task_create_failed");
|
|
256
|
-
const task = await response.json() as { id: { taskId: string } };
|
|
257
|
-
return { taskId: task.id.taskId };
|
|
258
|
-
}
|
|
259
|
-
`,
|
|
260
|
-
{ flag: "wx" },
|
|
261
|
-
);
|
|
262
|
-
await writeFile(
|
|
263
|
-
resolve(
|
|
264
|
-
temporaryDirectory,
|
|
265
|
-
"src",
|
|
266
|
-
"app",
|
|
267
|
-
"webhooks",
|
|
268
|
-
"qiqi-customer.webhook.ts",
|
|
269
|
-
),
|
|
270
|
-
`import { ZHIKE_API_TOKEN, kv } from "@77-zhike/app-sdk/server";
|
|
271
|
-
import type { WorkspaceWebhookBody } from "@77-zhike/app-sdk/server";
|
|
272
|
-
|
|
273
|
-
export default async function qiqiCustomer(request: Request) {
|
|
274
|
-
const idempotencyKey = request.headers.get("idempotency-key");
|
|
275
|
-
if (!idempotencyKey) throw new Error("webhook_idempotency_key_missing");
|
|
276
|
-
if (await kv.get(\`delivery:\${idempotencyKey}\`)) {
|
|
277
|
-
return new Response(null, { status: 200 });
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const { events } = await request.json() as WorkspaceWebhookBody;
|
|
281
|
-
for (const event of events) {
|
|
282
|
-
if (event.eventType === "activity.updated") {
|
|
283
|
-
const response = await fetch(
|
|
284
|
-
\`https://api-clm.77hub.com/v1/activities/\${encodeURIComponent(event.id.activityId)}\`,
|
|
285
|
-
{ headers: { authorization: \`Bearer \${ZHIKE_API_TOKEN}\` } },
|
|
286
|
-
);
|
|
287
|
-
if (!response.ok) throw new Error("activity_read_failed");
|
|
288
|
-
const activity = await response.json();
|
|
289
|
-
console.log("[qiqi-customer] current activity", activity);
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
if (event.eventType !== "record.created" || event.id.objectSlug !== "accounts") continue;
|
|
293
|
-
const response = await fetch(
|
|
294
|
-
\`https://api-clm.77hub.com/v1/objects/\${event.id.objectSlug}/records/\${event.id.recordId}\`,
|
|
295
|
-
{ headers: { authorization: \`Bearer \${ZHIKE_API_TOKEN}\` } },
|
|
296
|
-
);
|
|
297
|
-
if (!response.ok) throw new Error("record_read_failed");
|
|
298
|
-
const record = await response.json();
|
|
299
|
-
console.log("[qiqi-customer] current record values", record.values);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
await kv.set(\`delivery:\${idempotencyKey}\`, "completed", { ttlInSeconds: 172800 });
|
|
303
|
-
return new Response(null, { status: 200 });
|
|
304
|
-
}
|
|
305
|
-
`,
|
|
306
|
-
{ flag: "wx" },
|
|
307
|
-
);
|
|
308
139
|
await writeFile(
|
|
309
140
|
resolve(temporaryDirectory, "tsconfig.json"),
|
|
310
141
|
`${JSON.stringify(
|