@skopon-cool/form-sdk 0.2.1 → 0.2.3
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/README.md +94 -9
- package/dist/adapter/a2uiAdapter.d.ts +1 -0
- package/dist/adapter/a2uiAdapter.d.ts.map +1 -1
- package/dist/adapter/formSchema.d.ts +2 -1
- package/dist/adapter/formSchema.d.ts.map +1 -1
- package/dist/catalog/a2uiCustomCatalog.d.ts.map +1 -1
- package/dist/client/apiEnvelope.d.ts +7 -0
- package/dist/client/apiEnvelope.d.ts.map +1 -0
- package/dist/client/authStorage.d.ts +24 -0
- package/dist/client/authStorage.d.ts.map +1 -0
- package/dist/client/caseSearchClient.d.ts +11 -0
- package/dist/client/caseSearchClient.d.ts.map +1 -0
- package/dist/client/formClient.d.ts +2 -2
- package/dist/client/formClient.d.ts.map +1 -1
- package/dist/client/resumeSearchClient.d.ts +9 -0
- package/dist/client/resumeSearchClient.d.ts.map +1 -0
- package/dist/client/skoponSdkClients.d.ts +22 -0
- package/dist/client/skoponSdkClients.d.ts.map +1 -0
- package/dist/components/AskUserFormCard.d.ts +3 -1
- package/dist/components/AskUserFormCard.d.ts.map +1 -1
- package/dist/components/CurlSubmitBlock.d.ts +3 -1
- package/dist/components/CurlSubmitBlock.d.ts.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1191 -820
- package/dist/submit/buildAnswerAgentBody.d.ts +19 -0
- package/dist/submit/buildAnswerAgentBody.d.ts.map +1 -0
- package/dist/submit/submitAnswerAgent.d.ts +9 -0
- package/dist/submit/submitAnswerAgent.d.ts.map +1 -0
- package/dist/submit/validateFormSubmit.d.ts +10 -0
- package/dist/submit/validateFormSubmit.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/adapter/a2uiAdapter.ts +1 -1
- package/src/adapter/formSchema.test.ts +24 -0
- package/src/adapter/formSchema.ts +9 -2
- package/src/adapter/resolveSurface.ts +3 -3
- package/src/catalog/a2uiCustomCatalog.tsx +35 -3
- package/src/catalog/textFieldPreview.test.tsx +31 -0
- package/src/client/apiEnvelope.ts +21 -0
- package/src/client/authStorage.test.ts +87 -0
- package/src/client/authStorage.ts +102 -0
- package/src/client/caseSearchClient.ts +107 -0
- package/src/client/formClient.test.ts +25 -0
- package/src/client/formClient.ts +7 -25
- package/src/client/resumeSearchClient.test.ts +87 -0
- package/src/client/resumeSearchClient.ts +108 -0
- package/src/client/skoponSdkClients.ts +51 -0
- package/src/components/AskUserFormCard.tsx +45 -11
- package/src/components/CurlSubmitBlock.tsx +73 -25
- package/src/index.ts +32 -0
- package/src/submit/buildAnswerAgentBody.ts +61 -0
- package/src/submit/submit.test.ts +115 -0
- package/src/submit/submitAnswerAgent.ts +39 -0
- package/src/submit/validateFormSubmit.ts +111 -0
- package/dist/catalog/skoponSelectLoading.test.d.ts +0 -2
- package/dist/catalog/skoponSelectLoading.test.d.ts.map +0 -1
- package/dist/catalog/textFieldPreview.test.d.ts +0 -2
- package/dist/catalog/textFieldPreview.test.d.ts.map +0 -1
- package/dist/components/SkoponA2uiStreamRenderer.test.d.ts +0 -2
- package/dist/components/SkoponA2uiStreamRenderer.test.d.ts.map +0 -1
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
buildAskUserSubmitCurlFromPayload,
|
|
9
9
|
buildCurlStatement,
|
|
10
10
|
} from '../submit/buildCurlStatement'
|
|
11
|
+
import { buildAnswerAgentBody } from '../submit/buildAnswerAgentBody'
|
|
11
12
|
import {
|
|
12
13
|
extractExtraBlockValues,
|
|
13
14
|
getPayloadInputFieldNames,
|
|
@@ -16,7 +17,9 @@ import {
|
|
|
16
17
|
parsePayloadBlocksJson,
|
|
17
18
|
payloadHasInputBlocks,
|
|
18
19
|
} from '../submit/intersectPayloadBlocksWithForm'
|
|
20
|
+
import { submitAnswerAgent } from '../submit/submitAnswerAgent'
|
|
19
21
|
import { submitFormJson } from '../submit/submitFormJson'
|
|
22
|
+
import { validateFormSubmitValues } from '../submit/validateFormSubmit'
|
|
20
23
|
import type { FormSchema } from '../types/index'
|
|
21
24
|
|
|
22
25
|
describe('buildCurlStatement', () => {
|
|
@@ -260,6 +263,79 @@ describe('payload fallback helpers', () => {
|
|
|
260
263
|
})
|
|
261
264
|
})
|
|
262
265
|
|
|
266
|
+
describe('buildAnswerAgentBody', () => {
|
|
267
|
+
it('maps context and payload into answer-agent body', () => {
|
|
268
|
+
const payload = {
|
|
269
|
+
title: '需求描述',
|
|
270
|
+
blocks: [
|
|
271
|
+
{ id: 'b1', type: 'textarea', name: 'field_b6ba0dbb', label: '需求描述' },
|
|
272
|
+
],
|
|
273
|
+
}
|
|
274
|
+
const body = buildAnswerAgentBody({
|
|
275
|
+
context: {
|
|
276
|
+
missionId: 12,
|
|
277
|
+
tenantId: 1001,
|
|
278
|
+
tenantUserId: 'user-1',
|
|
279
|
+
requestId: 'req-1',
|
|
280
|
+
askUserQueryDescription: '请确认',
|
|
281
|
+
callbackUrl: 'https://example.com/cb',
|
|
282
|
+
},
|
|
283
|
+
payload,
|
|
284
|
+
answer: { field_b6ba0dbb: 'hello' },
|
|
285
|
+
})
|
|
286
|
+
expect(body.mission_id).toBe(12)
|
|
287
|
+
expect(body.tenant_id).toBe(1001)
|
|
288
|
+
expect(body.tenant_user_id).toBe('user-1')
|
|
289
|
+
expect(body.request_id).toBe('req-1')
|
|
290
|
+
expect(body.ask_user_query_description).toBe('请确认')
|
|
291
|
+
expect(body.callback_url).toBe('https://example.com/cb')
|
|
292
|
+
expect(body.answer).toEqual({ field_b6ba0dbb: 'hello' })
|
|
293
|
+
expect(body.question).toMatchObject({ title: '需求描述' })
|
|
294
|
+
expect(body.form).toMatchObject({ title: '需求描述' })
|
|
295
|
+
expect((body.question as { blocks: Array<{ name: string }> }).blocks[0]?.name).toBe(
|
|
296
|
+
'field_b6ba0dbb',
|
|
297
|
+
)
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
describe('submitAnswerAgent', () => {
|
|
302
|
+
it('POSTs to explicit answer agent callback url with auth headers', async () => {
|
|
303
|
+
const fetchMock = vi.fn(async () => ({
|
|
304
|
+
ok: true,
|
|
305
|
+
status: 200,
|
|
306
|
+
headers: { get: () => 'application/json' },
|
|
307
|
+
json: async () => ({ callback_ok: true }),
|
|
308
|
+
})) as unknown as typeof fetch
|
|
309
|
+
|
|
310
|
+
const result = await submitAnswerAgent(
|
|
311
|
+
{ answer: { a: 1 } },
|
|
312
|
+
{
|
|
313
|
+
answerAgentCallbackUrl: '/api/v1/mock/ask_user_answer_mock',
|
|
314
|
+
headers: { Authorization: 'Bearer tok' },
|
|
315
|
+
fetch: fetchMock,
|
|
316
|
+
},
|
|
317
|
+
)
|
|
318
|
+
expect(result.ok).toBe(true)
|
|
319
|
+
expect(fetchMock).toHaveBeenCalledWith(
|
|
320
|
+
'/api/v1/mock/ask_user_answer_mock',
|
|
321
|
+
expect.objectContaining({
|
|
322
|
+
method: 'POST',
|
|
323
|
+
headers: expect.objectContaining({
|
|
324
|
+
Authorization: 'Bearer tok',
|
|
325
|
+
'Content-Type': 'application/json',
|
|
326
|
+
}),
|
|
327
|
+
body: JSON.stringify({ answer: { a: 1 } }),
|
|
328
|
+
}),
|
|
329
|
+
)
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
it('throws when answer agent callback url is missing', async () => {
|
|
333
|
+
await expect(submitAnswerAgent({ answer: {} })).rejects.toThrow(
|
|
334
|
+
'answer_agent_callback_url 未配置,无法提交',
|
|
335
|
+
)
|
|
336
|
+
})
|
|
337
|
+
})
|
|
338
|
+
|
|
263
339
|
describe('submitFormJson', () => {
|
|
264
340
|
it('POSTs JSON payload', async () => {
|
|
265
341
|
const fetchMock = vi.fn(async () => ({
|
|
@@ -284,3 +360,42 @@ describe('submitFormJson', () => {
|
|
|
284
360
|
)
|
|
285
361
|
})
|
|
286
362
|
})
|
|
363
|
+
|
|
364
|
+
describe('validateFormSubmitValues', () => {
|
|
365
|
+
it('rejects empty required text field', () => {
|
|
366
|
+
const blocks = [
|
|
367
|
+
{ id: 'b1', type: 'text' as const, name: 'title', label: '标题', required: true },
|
|
368
|
+
]
|
|
369
|
+
expect(validateFormSubmitValues(blocks, { title: '' })?.message).toBe('请填写「标题」')
|
|
370
|
+
expect(validateFormSubmitValues(blocks, { title: 'ok' })).toBeNull()
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
it('rejects invalid email format', () => {
|
|
374
|
+
const blocks = [
|
|
375
|
+
{ id: 'b1', type: 'email' as const, name: 'email', label: '邮箱' },
|
|
376
|
+
]
|
|
377
|
+
expect(validateFormSubmitValues(blocks, { email: 'bad' })?.message).toBe('「邮箱」格式不正确')
|
|
378
|
+
expect(validateFormSubmitValues(blocks, { email: 'a@b.co' })).toBeNull()
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
it('enforces resume and case minCount when required or partially filled', () => {
|
|
382
|
+
const resumeBlocks = [
|
|
383
|
+
{
|
|
384
|
+
id: 'b1',
|
|
385
|
+
type: 'resume_multiselect' as const,
|
|
386
|
+
name: 'resumes',
|
|
387
|
+
label: '简历',
|
|
388
|
+
resumeMinCount: 2,
|
|
389
|
+
},
|
|
390
|
+
]
|
|
391
|
+
expect(validateFormSubmitValues(resumeBlocks, { resumes: [] })).toBeNull()
|
|
392
|
+
expect(validateFormSubmitValues(resumeBlocks, { resumes: ['r1'] })?.message).toBe(
|
|
393
|
+
'「简历」至少选择 2 项',
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
const requiredResume = [{ ...resumeBlocks[0]!, required: true }]
|
|
397
|
+
expect(validateFormSubmitValues(requiredResume, { resumes: [] })?.message).toBe(
|
|
398
|
+
'请填写「简历」',
|
|
399
|
+
)
|
|
400
|
+
})
|
|
401
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolveAnswerAgentCallbackUrl,
|
|
3
|
+
resolveSkoponAuthHeaders,
|
|
4
|
+
type SkoponAuthStorageOptions,
|
|
5
|
+
} from '../client/authStorage'
|
|
6
|
+
import {
|
|
7
|
+
submitFormJson,
|
|
8
|
+
type SubmitFormJsonOptions,
|
|
9
|
+
type SubmitFormJsonResult,
|
|
10
|
+
} from './submitFormJson'
|
|
11
|
+
|
|
12
|
+
export interface SubmitAnswerAgentOptions extends SubmitFormJsonOptions {
|
|
13
|
+
authStorage?: SkoponAuthStorageOptions
|
|
14
|
+
/** 显式覆盖 auth_user 中的 answer_agent_callback_url */
|
|
15
|
+
answerAgentCallbackUrl?: string | null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function submitAnswerAgent(
|
|
19
|
+
body: Record<string, unknown>,
|
|
20
|
+
options?: SubmitAnswerAgentOptions,
|
|
21
|
+
): Promise<SubmitFormJsonResult> {
|
|
22
|
+
const url = (
|
|
23
|
+
options?.answerAgentCallbackUrl ??
|
|
24
|
+
resolveAnswerAgentCallbackUrl(options?.authStorage)
|
|
25
|
+
).trim()
|
|
26
|
+
if (!url) {
|
|
27
|
+
throw new Error('answer_agent_callback_url 未配置,无法提交')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const storageHeaders = resolveSkoponAuthHeaders(options?.authStorage)
|
|
31
|
+
|
|
32
|
+
return submitFormJson(url, body, {
|
|
33
|
+
...options,
|
|
34
|
+
headers: {
|
|
35
|
+
...storageHeaders,
|
|
36
|
+
...(options?.headers ?? {}),
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { FormBlock } from '../types/index'
|
|
2
|
+
import { isLayoutBlockType, isMediaBlockType } from '../types/index'
|
|
3
|
+
import { EMAIL_REGEXP } from '../adapter/a2uiAdapter'
|
|
4
|
+
import { coerceToggleValue } from '../adapter/formSchema'
|
|
5
|
+
import { asStringArray } from '../catalog/useSkoponBoundField'
|
|
6
|
+
|
|
7
|
+
export interface FormSubmitValidationError {
|
|
8
|
+
fieldName: string
|
|
9
|
+
label: string
|
|
10
|
+
message: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function matchesValidationRegexp(value: string, pattern: string): boolean {
|
|
14
|
+
if (!pattern.trim()) return true
|
|
15
|
+
try {
|
|
16
|
+
return new RegExp(pattern).test(value)
|
|
17
|
+
} catch {
|
|
18
|
+
return true
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isBlankString(raw: unknown): boolean {
|
|
23
|
+
if (raw === undefined || raw === null) return true
|
|
24
|
+
if (typeof raw === 'string') return !raw.trim()
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isEmptySubmitValue(block: FormBlock, raw: unknown): boolean {
|
|
29
|
+
switch (block.type) {
|
|
30
|
+
case 'multiselect':
|
|
31
|
+
case 'checkbox':
|
|
32
|
+
case 'file':
|
|
33
|
+
case 'resume_multiselect':
|
|
34
|
+
case 'case_multiselect':
|
|
35
|
+
return asStringArray(raw).length === 0
|
|
36
|
+
case 'toggle':
|
|
37
|
+
return !coerceToggleValue(raw)
|
|
38
|
+
case 'number':
|
|
39
|
+
return raw === undefined || raw === null || raw === ''
|
|
40
|
+
default:
|
|
41
|
+
return isBlankString(raw)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function shouldEnforceMinCount(block: FormBlock, count: number): boolean {
|
|
46
|
+
return block.required === true || count > 0
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 提交前校验 renderBlocks 对应字段值;首个错误即返回。 */
|
|
50
|
+
export function validateFormSubmitValues(
|
|
51
|
+
blocks: FormBlock[],
|
|
52
|
+
values: Record<string, unknown>,
|
|
53
|
+
): FormSubmitValidationError | null {
|
|
54
|
+
for (const block of blocks) {
|
|
55
|
+
const name = block.name?.trim()
|
|
56
|
+
if (!name) continue
|
|
57
|
+
if (isLayoutBlockType(block.type) || isMediaBlockType(block.type)) continue
|
|
58
|
+
|
|
59
|
+
const raw = values[name]
|
|
60
|
+
const label = block.label?.trim() || name
|
|
61
|
+
|
|
62
|
+
if (block.required && isEmptySubmitValue(block, raw)) {
|
|
63
|
+
return { fieldName: name, label, message: `请填写「${label}」` }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (block.type === 'email' && !isBlankString(raw)) {
|
|
67
|
+
const text = String(raw).trim()
|
|
68
|
+
if (!matchesValidationRegexp(text, EMAIL_REGEXP)) {
|
|
69
|
+
return { fieldName: name, label, message: `「${label}」格式不正确` }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (block.type === 'file') {
|
|
74
|
+
const min = block.fileMinCount ?? 0
|
|
75
|
+
const count = asStringArray(raw).length
|
|
76
|
+
if (min > 0 && count < min && shouldEnforceMinCount(block, count)) {
|
|
77
|
+
return {
|
|
78
|
+
fieldName: name,
|
|
79
|
+
label,
|
|
80
|
+
message: `「${label}」至少需要 ${min} 个文件`,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (block.type === 'resume_multiselect') {
|
|
86
|
+
const min = block.resumeMinCount
|
|
87
|
+
const count = asStringArray(raw).length
|
|
88
|
+
if (min !== undefined && count < min && shouldEnforceMinCount(block, count)) {
|
|
89
|
+
return {
|
|
90
|
+
fieldName: name,
|
|
91
|
+
label,
|
|
92
|
+
message: `「${label}」至少选择 ${min} 项`,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (block.type === 'case_multiselect') {
|
|
98
|
+
const min = block.caseMinCount
|
|
99
|
+
const count = asStringArray(raw).length
|
|
100
|
+
if (min !== undefined && count < min && shouldEnforceMinCount(block, count)) {
|
|
101
|
+
return {
|
|
102
|
+
fieldName: name,
|
|
103
|
+
label,
|
|
104
|
+
message: `「${label}」至少选择 ${min} 项`,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"skoponSelectLoading.test.d.ts","sourceRoot":"","sources":["../../src/catalog/skoponSelectLoading.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"textFieldPreview.test.d.ts","sourceRoot":"","sources":["../../src/catalog/textFieldPreview.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SkoponA2uiStreamRenderer.test.d.ts","sourceRoot":"","sources":["../../src/components/SkoponA2uiStreamRenderer.test.tsx"],"names":[],"mappings":""}
|