This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.ts
More file actions
48 lines (39 loc) · 1.38 KB
/
assets.ts
File metadata and controls
48 lines (39 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import FormData from 'form-data'
import * as fs from 'fs'
import { basename } from 'path'
import { post } from './request'
import { CreateArticleAssetResponse } from './types/helpscout-docs'
import { Stream } from 'stream'
type AssetTypeParameter = 'image' | 'attachment'
interface CreateArticleAssetOptionsBase {
key: string
articleId: string
assetType: AssetTypeParameter
fileName?: string
}
type CreateArticleAssetOptionWithFilepath = CreateArticleAssetOptionsBase & {
filepath: string
}
type CreateArticleAssetOptionsWithStream = CreateArticleAssetOptionsBase & {
stream: Stream
}
export type CreateArticleAssetOptions = CreateArticleAssetOptionWithFilepath | CreateArticleAssetOptionsWithStream
export async function createArticleAsset(apiToken: string, options: CreateArticleAssetOptions) {
const form = new FormData()
form.append('key', apiToken)
form.append('articleId', options.articleId)
form.append('assetType', options.assetType)
if ('filepath' in options) {
form.append('file', fs.createReadStream(options.filepath), basename(options.filepath))
} else if ('stream' in options) {
form.append('file', options.stream)
}
if (options.fileName) {
form.append('fileName', options.fileName)
}
const response = await post<CreateArticleAssetResponse>(apiToken, `assets/article`, {
body: form,
headers: form.getHeaders(),
})
return response.body
}