Skip to content

Commit 5d54ca1

Browse files
committed
feat: add survey curl example code
1 parent 29f184c commit 5d54ca1

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

src/client/components/survey/SurveyUsageBtn.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import { LuCode2 } from 'react-icons/lu';
1313
import { trpc } from '@/api/trpc';
1414
import { useCurrentWorkspaceId } from '@/store/user';
1515
import { CodeBlock } from '../CodeBlock';
16-
import { generateSurveyExampleCode } from '@/utils/survey';
16+
import {
17+
generateSurveyExampleCurlCode,
18+
generateSurveyExampleSDKCode,
19+
} from '@/utils/survey';
20+
import { CodeExample } from '../CodeExample';
1721

1822
interface SurveyUsageBtnProps {
1923
surveyId: string;
@@ -29,8 +33,6 @@ export const SurveyUsageBtn: React.FC<SurveyUsageBtnProps> = React.memo(
2933
surveyId,
3034
});
3135

32-
const exampleCode = generateSurveyExampleCode(window.location.origin, info);
33-
3436
return (
3537
<Dialog>
3638
<DialogTrigger asChild>
@@ -46,9 +48,33 @@ export const SurveyUsageBtn: React.FC<SurveyUsageBtnProps> = React.memo(
4648
</DialogDescription>
4749
</DialogHeader>
4850

49-
<CodeBlock code="npm install tianji-client-sdk" />
51+
<CodeExample
52+
className="overflow-hidden"
53+
example={{
54+
curl: {
55+
label: 'curl',
56+
code: generateSurveyExampleCurlCode(
57+
window.location.origin,
58+
info
59+
),
60+
},
61+
sdk: {
62+
label: 'sdk',
63+
element: (
64+
<div className="flex flex-col gap-1">
65+
<CodeBlock code="npm install tianji-client-sdk" />
5066

51-
<CodeBlock code={exampleCode} />
67+
<CodeBlock
68+
code={generateSurveyExampleSDKCode(
69+
window.location.origin,
70+
info
71+
)}
72+
/>
73+
</div>
74+
),
75+
},
76+
}}
77+
/>
5278
</DialogContent>
5379
</Dialog>
5480
);

src/client/utils/__snapshots__/survey.spec.ts.snap

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`survey > example code 1`] = `
3+
exports[`survey > example curl code 1`] = `
4+
"curl -X POST https://example.com/open/workspace/<workspaceId>/survey/<surveyId>/submit \\
5+
-H "Content-Type: application/json" \\
6+
-d '{
7+
"payload": {
8+
"textField": "Text"
9+
}
10+
}'"
11+
`;
12+
13+
exports[`survey > example sdk code 1`] = `
414
"import { submitSurvey, initOpenapiSDK } from 'tianji-client-sdk';
515
616
initOpenapiSDK('https://example.com');

src/client/utils/survey.spec.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
import { describe, test, expect } from 'vitest';
2-
import { generateSurveyExampleCode } from './survey';
2+
import {
3+
generateSurveyExampleCurlCode,
4+
generateSurveyExampleSDKCode,
5+
} from './survey';
36

47
describe('survey', () => {
5-
test('example code', () => {
8+
test('example sdk code', () => {
69
expect(
7-
generateSurveyExampleCode('https://example.com', {
10+
generateSurveyExampleSDKCode('https://example.com', {
11+
id: '<surveyId>',
12+
workspaceId: '<workspaceId>',
13+
name: 'Test',
14+
payload: {
15+
items: [
16+
{
17+
name: 'textField',
18+
label: 'Text',
19+
type: 'text',
20+
},
21+
],
22+
},
23+
})
24+
).matchSnapshot();
25+
});
26+
27+
test('example curl code', () => {
28+
expect(
29+
generateSurveyExampleCurlCode('https://example.com', {
830
id: '<surveyId>',
931
workspaceId: '<workspaceId>',
1032
name: 'Test',

src/client/utils/survey.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AppRouterOutput } from '@/api/trpc';
22

33
/**
4-
* Generate survey example code
4+
* Generate survey example sdk code
55
*/
6-
export function generateSurveyExampleCode(
6+
export function generateSurveyExampleSDKCode(
77
host: string,
88
info:
99
| Pick<
@@ -42,3 +42,29 @@ async function submitForm(${fields.map((field) => field.name).join(', ')}) {
4242

4343
return exampleCode;
4444
}
45+
46+
/**
47+
* Generate survey example curl code
48+
*/
49+
export function generateSurveyExampleCurlCode(
50+
host: string,
51+
info:
52+
| Pick<
53+
NonNullable<AppRouterOutput['survey']['get']>,
54+
'id' | 'name' | 'workspaceId' | 'payload'
55+
>
56+
| null
57+
| undefined
58+
): string {
59+
const fields = info?.payload.items ?? [];
60+
61+
const exampleCode = `curl -X POST ${host}/open/workspace/${info?.workspaceId}/survey/${info?.id}/submit \\
62+
-H "Content-Type: application/json" \\
63+
-d '{
64+
"payload": {
65+
${fields.map((field) => `"${field.name}": "${field.label}"`).join(',\n ')}
66+
}
67+
}'`;
68+
69+
return exampleCode;
70+
}

src/client/vite.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default defineConfig({
2424
},
2525
clearScreen: false,
2626
server: {
27-
// host: '0.0.0.0',
27+
host: '127.0.0.1',
2828
proxy: {
2929
'/socket.io': {
3030
target: 'ws://localhost:12345',
@@ -34,6 +34,9 @@ export default defineConfig({
3434
'/trpc': {
3535
target: 'http://localhost:12345',
3636
},
37+
'/open': {
38+
target: 'http://localhost:12345',
39+
},
3740
'/lh': {
3841
target: 'http://localhost:12345',
3942
},

0 commit comments

Comments
 (0)