forked from Dituon/petpet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.js
77 lines (64 loc) · 1.83 KB
/
get.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
70
71
72
73
74
75
76
77
/**
* @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 GET
* @param { PetRequestDTO } param
* @return { Promise<Blob> } Image Blob
*/
const fetchImage = async param => {
if (!param.key) throw new Error('Param Key undefined')
let {key, randomAvatarList, textList, ...targets} = param
const urlParam = new URLSearchParams()
urlParam.append('key', key)
!randomAvatarList || urlParam.append('randomAvatarList', randomAvatarList)
!textList || urlParam.append('textList', textList)
Object.entries(targets).forEach(target => {
let [targetKey, targetObj] = target
let {name, avatar} = targetObj
urlParam.append(`${targetKey}Name`, name)
urlParam.append(`${targetKey}Avatar`, avatar)
})
let url = new URL('http://127.0.0.1:2333/petpet')
url.search = urlParam
return fetch(url).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)
*/
})