From 8565a1f4af87f66e5e631fa27b84639b8683ed35 Mon Sep 17 00:00:00 2001 From: ttang Date: Wed, 5 Jul 2023 15:23:40 +0800 Subject: [PATCH] feat: sdk with definition --- .../dashboard/src/components/ModelSDK.tsx | 19 ++++++++++++++- .../dashboard/src/components/sdkTemplate.ts | 24 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/client/dashboard/src/components/ModelSDK.tsx b/packages/client/dashboard/src/components/ModelSDK.tsx index a83f24b1..c078868f 100644 --- a/packages/client/dashboard/src/components/ModelSDK.tsx +++ b/packages/client/dashboard/src/components/ModelSDK.tsx @@ -77,17 +77,34 @@ export default function ModelSDK({ throw new Error('no graphql') } + const runtimeComposite = data.find( + (item) => item.filename === 'runtime-composite.ts' + ) + if (!runtimeComposite) { + throw new Error('no runtime-composite') + } + + runtimeComposite.content = `// This is an auto-generated file, do not edit manually + +export const definition = ${JSON.stringify( + JSON.parse(runtimeComposite.content), + null, + 2 + )} + ` + const sdkContent = sdkTemplate .replaceAll('<%= modelName %>', modelName) .replaceAll('<%= modelNameCamelcase %>', camelCase(modelName)) setCodes( [ - graphql, { filename: `S3${modelName}Model.ts`, content: sdkContent, }, + runtimeComposite, + graphql, ].map((item, i) => ({ title: item.filename, content: item.content, diff --git a/packages/client/dashboard/src/components/sdkTemplate.ts b/packages/client/dashboard/src/components/sdkTemplate.ts index f24cbf7e..03e0c5e9 100644 --- a/packages/client/dashboard/src/components/sdkTemplate.ts +++ b/packages/client/dashboard/src/components/sdkTemplate.ts @@ -1,4 +1,26 @@ -export const sdkTemplate = ` +export const sdkTemplate = `/** + * How to use this model: + * + * export const CERAMIC_TESTNET_HOST = "https://gcp-ceramic-testnet-dev.s3.xyz"; + * const <%= modelNameCamelcase %>Model = new S3<%= modelName %>Model(CERAMIC_TESTNET_HOST); + * + * // auth with didSession + * <%= modelNameCamelcase %>Model.authComposeClient(didSession); + * + * // createNew + * const resp = await <%= modelNameCamelcase %>Model.create<%= modelName %>({...}); + * + * // update + * const resp = await <%= modelNameCamelcase %>Model.update<%= modelName %>({...}); + * + * // queryList + * const resp = await <%= modelNameCamelcase %>Model.query<%= modelName %>Index({first: 100, after: ""}); + * + * // queryWithId + * const resp = await <%= modelNameCamelcase %>Model.query<%= modelName %>WithId("..."); + * + */ + import { ComposeClient } from "@composedb/client"; import { RuntimeCompositeDefinition } from "@composedb/types"; import { DIDSession } from "did-session";