diff --git a/src/client/components/survey/SurveyUsageBtn.tsx b/src/client/components/survey/SurveyUsageBtn.tsx index 361219f..6113993 100644 --- a/src/client/components/survey/SurveyUsageBtn.tsx +++ b/src/client/components/survey/SurveyUsageBtn.tsx @@ -13,7 +13,11 @@ import { LuCode2 } from 'react-icons/lu'; import { trpc } from '@/api/trpc'; import { useCurrentWorkspaceId } from '@/store/user'; import { CodeBlock } from '../CodeBlock'; -import { generateSurveyExampleCode } from '@/utils/survey'; +import { + generateSurveyExampleCurlCode, + generateSurveyExampleSDKCode, +} from '@/utils/survey'; +import { CodeExample } from '../CodeExample'; interface SurveyUsageBtnProps { surveyId: string; @@ -29,8 +33,6 @@ export const SurveyUsageBtn: React.FC = React.memo( surveyId, }); - const exampleCode = generateSurveyExampleCode(window.location.origin, info); - return ( @@ -46,9 +48,33 @@ export const SurveyUsageBtn: React.FC = React.memo( - + + - + + + ), + }, + }} + /> ); diff --git a/src/client/utils/__snapshots__/survey.spec.ts.snap b/src/client/utils/__snapshots__/survey.spec.ts.snap index 6432b36..3b4b046 100644 --- a/src/client/utils/__snapshots__/survey.spec.ts.snap +++ b/src/client/utils/__snapshots__/survey.spec.ts.snap @@ -1,6 +1,16 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`survey > example code 1`] = ` +exports[`survey > example curl code 1`] = ` +"curl -X POST https://example.com/open/workspace//survey//submit \\ + -H "Content-Type: application/json" \\ + -d '{ + "payload": { + "textField": "Text" + } + }'" +`; + +exports[`survey > example sdk code 1`] = ` "import { submitSurvey, initOpenapiSDK } from 'tianji-client-sdk'; initOpenapiSDK('https://example.com'); diff --git a/src/client/utils/survey.spec.ts b/src/client/utils/survey.spec.ts index 32bb934..642eebb 100644 --- a/src/client/utils/survey.spec.ts +++ b/src/client/utils/survey.spec.ts @@ -1,10 +1,32 @@ import { describe, test, expect } from 'vitest'; -import { generateSurveyExampleCode } from './survey'; +import { + generateSurveyExampleCurlCode, + generateSurveyExampleSDKCode, +} from './survey'; describe('survey', () => { - test('example code', () => { + test('example sdk code', () => { expect( - generateSurveyExampleCode('https://example.com', { + generateSurveyExampleSDKCode('https://example.com', { + id: '', + workspaceId: '', + name: 'Test', + payload: { + items: [ + { + name: 'textField', + label: 'Text', + type: 'text', + }, + ], + }, + }) + ).matchSnapshot(); + }); + + test('example curl code', () => { + expect( + generateSurveyExampleCurlCode('https://example.com', { id: '', workspaceId: '', name: 'Test', diff --git a/src/client/utils/survey.ts b/src/client/utils/survey.ts index b74cd96..de540c9 100644 --- a/src/client/utils/survey.ts +++ b/src/client/utils/survey.ts @@ -1,9 +1,9 @@ import { AppRouterOutput } from '@/api/trpc'; /** - * Generate survey example code + * Generate survey example sdk code */ -export function generateSurveyExampleCode( +export function generateSurveyExampleSDKCode( host: string, info: | Pick< @@ -42,3 +42,29 @@ async function submitForm(${fields.map((field) => field.name).join(', ')}) { return exampleCode; } + +/** + * Generate survey example curl code + */ +export function generateSurveyExampleCurlCode( + host: string, + info: + | Pick< + NonNullable, + 'id' | 'name' | 'workspaceId' | 'payload' + > + | null + | undefined +): string { + const fields = info?.payload.items ?? []; + + const exampleCode = `curl -X POST ${host}/open/workspace/${info?.workspaceId}/survey/${info?.id}/submit \\ + -H "Content-Type: application/json" \\ + -d '{ + "payload": { + ${fields.map((field) => `"${field.name}": "${field.label}"`).join(',\n ')} + } + }'`; + + return exampleCode; +} diff --git a/src/client/vite.config.ts b/src/client/vite.config.ts index 98fa180..03a6b1e 100644 --- a/src/client/vite.config.ts +++ b/src/client/vite.config.ts @@ -24,7 +24,7 @@ export default defineConfig({ }, clearScreen: false, server: { - // host: '0.0.0.0', + host: '127.0.0.1', proxy: { '/socket.io': { target: 'ws://localhost:12345', @@ -34,6 +34,9 @@ export default defineConfig({ '/trpc': { target: 'http://localhost:12345', }, + '/open': { + target: 'http://localhost:12345', + }, '/lh': { target: 'http://localhost:12345', },