-
Notifications
You must be signed in to change notification settings - Fork 81
/
post.js
69 lines (59 loc) · 1.51 KB
/
post.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @typedef { {name: string, avatar: string | URL} } TargetDTO
*/
/**
* @typedef { object } PetRequestDTO
* @property { string } key
* @property { TargetDTO } [from]
* @property { TargetDTO } [to]
* @property { TargetDTO } [bot]
* @property { TargetDTO } [group]
* @property { string[] | URL[] } randomAvatarList
* @property { string[] } textList
*/
/**
* fetch Image by POST
* @param { PetRequestDTO } param
* @return { Promise<Blob> } Image Blob
*/
const fetchImage = async param => {
if (!param.key) throw new Error('Param Key undefined')
param.randomAvatarList = param.randomAvatarList ?? []
param.textList = param.textList ?? []
return fetch('http://127.0.0.1:2333/petpet', {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(param)
}).then(p => p.blob())
}
// 👇示例脚本
/** @type { PetRequestDTO } */
const requestParam = {
key: 'petpet',
to: {
name: 'Dituon',
avatar: 'https://q1.qlogo.cn/g?b=qq&nk=2544193782&s=640'
},
// randomAvatarList: [],
// textList: []
}
fetchImage(requestParam).then(imageBlob => {
// do something
console.log('Hello, Petpet!')
/*
// DOM JS
const image = new Image()
image.src = URL.createObjectURL(imageBlob)
document.body.appendChild(image)
*/
/*
// Node JS
// ES Style
// import fs from 'fs'
// AMD Style
const fs = require('fs')
fs.writeFileSync('./petpet.gif', imageBlob)
*/
})