(config: {
kind: T;
onCreateDocument: (params: CreateDocumentCallbackProps) => Promise<string>;
onUpdateDocument: (params: UpdateDocumentCallbackProps) => Promise<string>;
})
| 38 | } |
| 39 | |
| 40 | export function createDocumentHandler<T extends ArtifactKind>(config: { |
| 41 | kind: T; |
| 42 | onCreateDocument: (params: CreateDocumentCallbackProps) => Promise<string>; |
| 43 | onUpdateDocument: (params: UpdateDocumentCallbackProps) => Promise<string>; |
| 44 | }): DocumentHandler<T> { |
| 45 | return { |
| 46 | kind: config.kind, |
| 47 | onCreateDocument: async (args: CreateDocumentCallbackProps) => { |
| 48 | const draftContent = await config.onCreateDocument({ |
| 49 | id: args.id, |
| 50 | title: args.title, |
| 51 | dataStream: args.dataStream, |
| 52 | session: args.session, |
| 53 | }); |
| 54 | |
| 55 | if (args.session?.user?.id) { |
| 56 | await saveDocument({ |
| 57 | id: args.id, |
| 58 | title: args.title, |
| 59 | content: draftContent, |
| 60 | kind: config.kind, |
| 61 | userId: args.session.user.id, |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | return; |
| 66 | }, |
| 67 | onUpdateDocument: async (args: UpdateDocumentCallbackProps) => { |
| 68 | const draftContent = await config.onUpdateDocument({ |
| 69 | document: args.document, |
| 70 | description: args.description, |
| 71 | dataStream: args.dataStream, |
| 72 | session: args.session, |
| 73 | }); |
| 74 | |
| 75 | if (args.session?.user?.id) { |
| 76 | await saveDocument({ |
| 77 | id: args.document.id, |
| 78 | title: args.document.title, |
| 79 | content: draftContent, |
| 80 | kind: config.kind, |
| 81 | userId: args.session.user.id, |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | return; |
| 86 | }, |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Use this array to define the document handlers for each artifact kind. |
no test coverage detected
searching dependent graphs…