@skopon-cool/form-sdk 0.1.5 → 0.1.6
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/dist/components/CurlSubmitBlock.d.ts +4 -1
- package/dist/components/CurlSubmitBlock.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +525 -516
- package/dist/submit/buildCurlStatement.d.ts +2 -0
- package/dist/submit/buildCurlStatement.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/CurlSubmitBlock.tsx +28 -7
- package/src/index.ts +6 -1
- package/src/submit/buildCurlStatement.ts +16 -0
- package/src/submit/submit.test.ts +32 -0
|
@@ -7,4 +7,6 @@ export interface BuildAskUserCurlStatementOptions {
|
|
|
7
7
|
callbackUrl?: string | null;
|
|
8
8
|
}
|
|
9
9
|
export declare function buildAskUserCurlStatement({ cardValues, extraValues, callbackUrl, }: BuildAskUserCurlStatementOptions): string;
|
|
10
|
+
/** 从 ask_user payload(blocksJson)生成向 callback 提交返回字段的 curl */
|
|
11
|
+
export declare function buildAskUserSubmitCurlFromPayload(payload: unknown, callbackUrl?: string | null): string;
|
|
10
12
|
//# sourceMappingURL=buildCurlStatement.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildCurlStatement.d.ts","sourceRoot":"","sources":["../../src/submit/buildCurlStatement.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"buildCurlStatement.d.ts","sourceRoot":"","sources":["../../src/submit/buildCurlStatement.ts"],"names":[],"mappings":"AASA,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAQxF;AAED,mDAAmD;AACnD,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACnC,MAAM,CAER;AAED,MAAM,WAAW,gCAAgC;IAC/C,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACrC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAC5B;AAED,wBAAgB,yBAAyB,CAAC,EACxC,UAAU,EACV,WAAgB,EAChB,WAAW,GACZ,EAAE,gCAAgC,GAAG,MAAM,CAQ3C;AAED,8DAA8D;AAC9D,wBAAgB,iCAAiC,CAC/C,OAAO,EAAE,OAAO,EAChB,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,GAC1B,MAAM,CAKR"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { useMemo } from 'react'
|
|
2
2
|
import { Button, Tag, Tooltip, Typography } from 'antd'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
buildAskUserSubmitCurlFromPayload,
|
|
5
|
+
buildCurlStatement,
|
|
6
|
+
} from '../submit/buildCurlStatement'
|
|
4
7
|
import { copyTextToClipboard } from '../submit/submitFormJson'
|
|
5
8
|
|
|
9
|
+
export type CurlSubmitBlockCopyMode = 'display' | 'submitFields'
|
|
10
|
+
|
|
6
11
|
export interface CurlSubmitBlockProps {
|
|
7
12
|
payload: unknown
|
|
8
13
|
callbackUrl?: string | null
|
|
9
14
|
title?: string
|
|
10
15
|
unpublishedFormId?: string | null
|
|
11
16
|
incompleteFormId?: string | null
|
|
17
|
+
/** display:复制与显示一致;submitFields:复制向 callback 提交返回字段的 curl */
|
|
18
|
+
copyMode?: CurlSubmitBlockCopyMode
|
|
12
19
|
onNotify?: (type: 'success' | 'error', message: string) => void
|
|
13
20
|
}
|
|
14
21
|
|
|
@@ -18,22 +25,34 @@ export default function CurlSubmitBlock({
|
|
|
18
25
|
title,
|
|
19
26
|
unpublishedFormId,
|
|
20
27
|
incompleteFormId,
|
|
28
|
+
copyMode = 'display',
|
|
21
29
|
onNotify,
|
|
22
30
|
}: CurlSubmitBlockProps) {
|
|
23
|
-
const
|
|
31
|
+
const displayCurl = useMemo(
|
|
24
32
|
() => buildCurlStatement(payload, callbackUrl),
|
|
25
33
|
[payload, callbackUrl],
|
|
26
34
|
)
|
|
35
|
+
const copyCurl = useMemo(() => {
|
|
36
|
+
if (copyMode === 'submitFields') {
|
|
37
|
+
return buildAskUserSubmitCurlFromPayload(payload, callbackUrl)
|
|
38
|
+
}
|
|
39
|
+
return displayCurl
|
|
40
|
+
}, [copyMode, payload, callbackUrl, displayCurl])
|
|
27
41
|
|
|
28
42
|
async function handleCopy() {
|
|
29
43
|
try {
|
|
30
|
-
await copyTextToClipboard(
|
|
44
|
+
await copyTextToClipboard(copyCurl)
|
|
31
45
|
onNotify?.('success', '已复制到剪贴板')
|
|
32
46
|
} catch {
|
|
33
47
|
onNotify?.('error', '复制失败')
|
|
34
48
|
}
|
|
35
49
|
}
|
|
36
50
|
|
|
51
|
+
const copyTooltip =
|
|
52
|
+
copyMode === 'submitFields'
|
|
53
|
+
? '复制提交字段 curl(向 callback 回传)'
|
|
54
|
+
: '复制 curl'
|
|
55
|
+
|
|
37
56
|
return (
|
|
38
57
|
<div className="ask-user-curl-card">
|
|
39
58
|
<div className="ask-user-curl-card-header">
|
|
@@ -50,11 +69,13 @@ export default function CurlSubmitBlock({
|
|
|
50
69
|
</Tooltip>
|
|
51
70
|
) : null}
|
|
52
71
|
</div>
|
|
53
|
-
<
|
|
54
|
-
|
|
55
|
-
|
|
72
|
+
<Tooltip title={copyTooltip}>
|
|
73
|
+
<Button size="small" type="text" onClick={() => void handleCopy()}>
|
|
74
|
+
复制
|
|
75
|
+
</Button>
|
|
76
|
+
</Tooltip>
|
|
56
77
|
</div>
|
|
57
|
-
<pre className="skopon-form-curl-json">{
|
|
78
|
+
<pre className="skopon-form-curl-json">{displayCurl}</pre>
|
|
58
79
|
</div>
|
|
59
80
|
)
|
|
60
81
|
}
|
package/src/index.ts
CHANGED
|
@@ -41,7 +41,12 @@ export { normalizeFormDefinition, syncFormDefinition } from './adapter/formSchem
|
|
|
41
41
|
|
|
42
42
|
export { extractSurfaceValues } from './adapter/extractSurfaceValues'
|
|
43
43
|
|
|
44
|
-
export {
|
|
44
|
+
export {
|
|
45
|
+
buildCurlStatement,
|
|
46
|
+
buildAskUserCurlStatement,
|
|
47
|
+
buildAskUserCurlBodyJson,
|
|
48
|
+
buildAskUserSubmitCurlFromPayload,
|
|
49
|
+
} from './submit/buildCurlStatement'
|
|
45
50
|
export {
|
|
46
51
|
extractExtraBlockValues,
|
|
47
52
|
getPayloadInputFieldNames,
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
extractExtraBlockValues,
|
|
3
|
+
parsePayloadBlocksJson,
|
|
4
|
+
} from './intersectPayloadBlocksWithForm'
|
|
5
|
+
|
|
1
6
|
function escapeSingleQuotes(text: string): string {
|
|
2
7
|
return text.replace(/'/g, `'\\''`)
|
|
3
8
|
}
|
|
@@ -39,3 +44,14 @@ export function buildAskUserCurlStatement({
|
|
|
39
44
|
` -d '${escapeSingleQuotes(body)}'`,
|
|
40
45
|
].join('\n')
|
|
41
46
|
}
|
|
47
|
+
|
|
48
|
+
/** 从 ask_user payload(blocksJson)生成向 callback 提交返回字段的 curl */
|
|
49
|
+
export function buildAskUserSubmitCurlFromPayload(
|
|
50
|
+
payload: unknown,
|
|
51
|
+
callbackUrl?: string | null,
|
|
52
|
+
): string {
|
|
53
|
+
const def = parsePayloadBlocksJson(payload)
|
|
54
|
+
if (!def) return buildCurlStatement(payload, callbackUrl)
|
|
55
|
+
const cardValues = extractExtraBlockValues(def.blocks)
|
|
56
|
+
return buildAskUserCurlStatement({ cardValues, callbackUrl })
|
|
57
|
+
}
|
|
@@ -5,6 +5,7 @@ import { describe, it, expect, vi } from 'vitest'
|
|
|
5
5
|
import {
|
|
6
6
|
buildAskUserCurlBodyJson,
|
|
7
7
|
buildAskUserCurlStatement,
|
|
8
|
+
buildAskUserSubmitCurlFromPayload,
|
|
8
9
|
buildCurlStatement,
|
|
9
10
|
} from '../submit/buildCurlStatement'
|
|
10
11
|
import {
|
|
@@ -54,6 +55,37 @@ describe('buildAskUserCurlStatement', () => {
|
|
|
54
55
|
})
|
|
55
56
|
})
|
|
56
57
|
|
|
58
|
+
describe('buildAskUserSubmitCurlFromPayload', () => {
|
|
59
|
+
it('builds submit-fields curl from blocksJson payload', () => {
|
|
60
|
+
const payload = {
|
|
61
|
+
title: 'Ask',
|
|
62
|
+
blocks: [
|
|
63
|
+
{ id: 'p1', type: 'text', name: 'video_title', label: '标题', defaultValue: 'hello' },
|
|
64
|
+
{ id: 'p2', type: 'text', name: 'agent_hint', label: '提示', defaultValue: 'hint' },
|
|
65
|
+
],
|
|
66
|
+
}
|
|
67
|
+
const curl = buildAskUserSubmitCurlFromPayload(payload, 'https://example.com/hook')
|
|
68
|
+
expect(curl).toContain("curl -X POST 'https://example.com/hook'")
|
|
69
|
+
expect(curl).toContain('"video_title": "hello"')
|
|
70
|
+
expect(curl).toContain('"agent_hint": "hint"')
|
|
71
|
+
const bodyMatch = curl.match(/-d '([\s\S]*)'$/)
|
|
72
|
+
expect(bodyMatch).toBeTruthy()
|
|
73
|
+
const bodyText = bodyMatch![1]!.replace(/'\\''/g, "'")
|
|
74
|
+
expect(JSON.parse(bodyText)).toEqual({
|
|
75
|
+
video_title: 'hello',
|
|
76
|
+
agent_hint: 'hint',
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('falls back to display curl when payload is not blocksJson', () => {
|
|
81
|
+
const payload = { video_title: { type: 'text' } }
|
|
82
|
+
const callbackUrl = 'https://example.com/hook'
|
|
83
|
+
expect(buildAskUserSubmitCurlFromPayload(payload, callbackUrl)).toBe(
|
|
84
|
+
buildCurlStatement(payload, callbackUrl),
|
|
85
|
+
)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
57
89
|
describe('parsePayloadBlocksJson', () => {
|
|
58
90
|
it('parses blocksJson payload', () => {
|
|
59
91
|
const parsed = parsePayloadBlocksJson({
|