Skip to content

Commit

Permalink
feat(satori): enhance blob support for internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 25, 2024
1 parent fab90c8 commit 8b20e2a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion adapters/satori/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-satori",
"description": "Satori Adapter for Satorijs",
"version": "1.4.0",
"version": "1.4.1",
"type": "module",
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion adapters/satori/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createInternal(bot: SatoriBot, prefix = '') {
return bot.http.post('/v1/internal/' + key, args)
}
const form = new FormData()
form.append('$', new Blob([JSON.stringify(data)], { type: 'application/json' }))
form.append('$', JSON.stringify(data))
for (const [key, value] of Object.entries(blobs)) {
if (value instanceof File) {
form.append(key, value, value.name)
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/plugin-server",
"description": "Basic API server for Satori protocol",
"version": "2.8.0",
"version": "2.8.2",
"type": "module",
"exports": {
".": {
Expand Down
17 changes: 6 additions & 11 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,12 @@ class SatoriServer extends Service<SatoriServer.Config> {
try {
let args = koa.request.body
if (koa.request.files) {
const blobs: Dict<Blob> = Object.create(null)
const { $, ...files } = koa.request.files
const [json] = await Promise.all([
readFile(makeArray($)[0].filepath, 'utf8'),
Promise.all(Object.entries(files).map(async ([key, value]) => {
value = makeArray(value)[0]
const buffer = await readFile(value.filepath)
return [key, new File([buffer], value.originalFilename!, { type: value.mimetype! })]
})),
])
args = deserialize(JSON.parse(json), '$', blobs)
const blobs = Object.fromEntries(await Promise.all(Object.entries(koa.request.files).map(async ([key, value]) => {
value = makeArray(value)[0]
const buffer = await readFile(value.filepath)
return [key, new File([buffer], value.originalFilename!, { type: value.mimetype! })] as const
})))
args = deserialize(JSON.parse(koa.request.body.$), '$', blobs)
}
const result = await bot.internal[name](...args)
koa.body = result
Expand Down

0 comments on commit 8b20e2a

Please sign in to comment.