feat: add survey curl example code
This commit is contained in:
parent
29f184c15d
commit
5d54ca1cbc
@ -13,7 +13,11 @@ import { LuCode2 } from 'react-icons/lu';
|
|||||||
import { trpc } from '@/api/trpc';
|
import { trpc } from '@/api/trpc';
|
||||||
import { useCurrentWorkspaceId } from '@/store/user';
|
import { useCurrentWorkspaceId } from '@/store/user';
|
||||||
import { CodeBlock } from '../CodeBlock';
|
import { CodeBlock } from '../CodeBlock';
|
||||||
import { generateSurveyExampleCode } from '@/utils/survey';
|
import {
|
||||||
|
generateSurveyExampleCurlCode,
|
||||||
|
generateSurveyExampleSDKCode,
|
||||||
|
} from '@/utils/survey';
|
||||||
|
import { CodeExample } from '../CodeExample';
|
||||||
|
|
||||||
interface SurveyUsageBtnProps {
|
interface SurveyUsageBtnProps {
|
||||||
surveyId: string;
|
surveyId: string;
|
||||||
@ -29,8 +33,6 @@ export const SurveyUsageBtn: React.FC<SurveyUsageBtnProps> = React.memo(
|
|||||||
surveyId,
|
surveyId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const exampleCode = generateSurveyExampleCode(window.location.origin, info);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog>
|
<Dialog>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
@ -46,9 +48,33 @@ export const SurveyUsageBtn: React.FC<SurveyUsageBtnProps> = React.memo(
|
|||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|
||||||
<CodeBlock code="npm install tianji-client-sdk" />
|
<CodeExample
|
||||||
|
className="overflow-hidden"
|
||||||
|
example={{
|
||||||
|
curl: {
|
||||||
|
label: 'curl',
|
||||||
|
code: generateSurveyExampleCurlCode(
|
||||||
|
window.location.origin,
|
||||||
|
info
|
||||||
|
),
|
||||||
|
},
|
||||||
|
sdk: {
|
||||||
|
label: 'sdk',
|
||||||
|
element: (
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<CodeBlock code="npm install tianji-client-sdk" />
|
||||||
|
|
||||||
<CodeBlock code={exampleCode} />
|
<CodeBlock
|
||||||
|
code={generateSurveyExampleSDKCode(
|
||||||
|
window.location.origin,
|
||||||
|
info
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
// 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/<workspaceId>/survey/<surveyId>/submit \\
|
||||||
|
-H "Content-Type: application/json" \\
|
||||||
|
-d '{
|
||||||
|
"payload": {
|
||||||
|
"textField": "Text"
|
||||||
|
}
|
||||||
|
}'"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`survey > example sdk code 1`] = `
|
||||||
"import { submitSurvey, initOpenapiSDK } from 'tianji-client-sdk';
|
"import { submitSurvey, initOpenapiSDK } from 'tianji-client-sdk';
|
||||||
|
|
||||||
initOpenapiSDK('https://example.com');
|
initOpenapiSDK('https://example.com');
|
||||||
|
@ -1,10 +1,32 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { generateSurveyExampleCode } from './survey';
|
import {
|
||||||
|
generateSurveyExampleCurlCode,
|
||||||
|
generateSurveyExampleSDKCode,
|
||||||
|
} from './survey';
|
||||||
|
|
||||||
describe('survey', () => {
|
describe('survey', () => {
|
||||||
test('example code', () => {
|
test('example sdk code', () => {
|
||||||
expect(
|
expect(
|
||||||
generateSurveyExampleCode('https://example.com', {
|
generateSurveyExampleSDKCode('https://example.com', {
|
||||||
|
id: '<surveyId>',
|
||||||
|
workspaceId: '<workspaceId>',
|
||||||
|
name: 'Test',
|
||||||
|
payload: {
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
name: 'textField',
|
||||||
|
label: 'Text',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).matchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('example curl code', () => {
|
||||||
|
expect(
|
||||||
|
generateSurveyExampleCurlCode('https://example.com', {
|
||||||
id: '<surveyId>',
|
id: '<surveyId>',
|
||||||
workspaceId: '<workspaceId>',
|
workspaceId: '<workspaceId>',
|
||||||
name: 'Test',
|
name: 'Test',
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { AppRouterOutput } from '@/api/trpc';
|
import { AppRouterOutput } from '@/api/trpc';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate survey example code
|
* Generate survey example sdk code
|
||||||
*/
|
*/
|
||||||
export function generateSurveyExampleCode(
|
export function generateSurveyExampleSDKCode(
|
||||||
host: string,
|
host: string,
|
||||||
info:
|
info:
|
||||||
| Pick<
|
| Pick<
|
||||||
@ -42,3 +42,29 @@ async function submitForm(${fields.map((field) => field.name).join(', ')}) {
|
|||||||
|
|
||||||
return exampleCode;
|
return exampleCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate survey example curl code
|
||||||
|
*/
|
||||||
|
export function generateSurveyExampleCurlCode(
|
||||||
|
host: string,
|
||||||
|
info:
|
||||||
|
| Pick<
|
||||||
|
NonNullable<AppRouterOutput['survey']['get']>,
|
||||||
|
'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;
|
||||||
|
}
|
||||||
|
@ -24,7 +24,7 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
clearScreen: false,
|
clearScreen: false,
|
||||||
server: {
|
server: {
|
||||||
// host: '0.0.0.0',
|
host: '127.0.0.1',
|
||||||
proxy: {
|
proxy: {
|
||||||
'/socket.io': {
|
'/socket.io': {
|
||||||
target: 'ws://localhost:12345',
|
target: 'ws://localhost:12345',
|
||||||
@ -34,6 +34,9 @@ export default defineConfig({
|
|||||||
'/trpc': {
|
'/trpc': {
|
||||||
target: 'http://localhost:12345',
|
target: 'http://localhost:12345',
|
||||||
},
|
},
|
||||||
|
'/open': {
|
||||||
|
target: 'http://localhost:12345',
|
||||||
|
},
|
||||||
'/lh': {
|
'/lh': {
|
||||||
target: 'http://localhost:12345',
|
target: 'http://localhost:12345',
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user