@satorijs/core
Advanced tools
Comparing version 4.3.4 to 4.4.0
@@ -207,2 +207,6 @@ import { Context, Service, z, Logger } from 'cordis'; | ||
} | ||
export namespace JsonForm { | ||
function load(data: any, path: string, form: FormData): any; | ||
function dump(data: any, path: string, form: FormData): any; | ||
} | ||
export interface Bot extends Methods { | ||
@@ -209,0 +213,0 @@ userId: string; |
{ | ||
"name": "@satorijs/core", | ||
"description": "Core components of Satorijs", | ||
"version": "4.3.4", | ||
"version": "4.4.0", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.cjs", |
import { Context, Logger, Service, z } from 'cordis' | ||
import { Awaitable, defineProperty, Dict } from 'cosmokit' | ||
import { Bot } from './bot' | ||
import { ExtractParams, InternalRequest, InternalRouter } from './internal' | ||
import { ExtractParams, InternalRequest, InternalRouter, JsonForm } from './internal' | ||
import { Session } from './session' | ||
@@ -202,2 +202,33 @@ import { HTTP } from '@cordisjs/plugin-http' | ||
}) | ||
ctx.satori.defineInternalRoute('/_api/:name', async ({ bot, headers, params, method, body }) => { | ||
if (method !== 'POST') return { status: 405 } | ||
const type = headers['content-type'] | ||
let args: any | ||
if (type?.startsWith('multipart/form-data')) { | ||
const response = new Response(body, { headers }) | ||
const form = await response.formData() | ||
const rawData = form.get('$') as string | ||
try { | ||
args = JSON.parse(rawData) | ||
} catch { | ||
return { status: 400 } | ||
} | ||
args = JsonForm.load(args, '$', form) | ||
} else { | ||
args = JSON.parse(new TextDecoder().decode(body)) | ||
} | ||
try { | ||
const result = await bot.internal[params.name](...args) | ||
const body = new TextEncoder().encode(JSON.stringify(result)) | ||
const headers = new Headers() | ||
if (body.byteLength) { | ||
headers.set('content-type', 'application/json') | ||
} | ||
return { body, headers, status: 200 } | ||
} catch (error) { | ||
if (!ctx.http.isError(error) || !error.response) throw error | ||
return error.response | ||
} | ||
}) | ||
} | ||
@@ -204,0 +235,0 @@ |
import { Service } from 'cordis' | ||
import { Dict, remove } from 'cosmokit' | ||
import { Dict, remove, valueMap } from 'cosmokit' | ||
import { HTTP } from '@cordisjs/plugin-http' | ||
@@ -108,1 +108,29 @@ import { Response } from '@satorijs/protocol' | ||
} | ||
export namespace JsonForm { | ||
export function load(data: any, path: string, form: FormData) { | ||
const value = form.get(path) | ||
if (value instanceof File) return value | ||
if (!data || typeof data !== 'object') return data | ||
if (Array.isArray(data)) { | ||
return data.map((value, index) => load(value, `${path}.${index}`, form)) | ||
} | ||
return valueMap(data, (value, key) => { | ||
return load(value, `${path}.${key}`, form) | ||
}) | ||
} | ||
export function dump(data: any, path: string, form: FormData) { | ||
if (!data || typeof data !== 'object') return data | ||
if (data instanceof Blob) { | ||
form.append(path, data) | ||
return null | ||
} | ||
if (Array.isArray(data)) { | ||
return data.map((value, index) => dump(value, `${path}.${index}`, form)) | ||
} | ||
return valueMap(data, (value, key) => { | ||
return dump(value, `${path}.${key}`, form) | ||
}) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
106673
3073