Skip to content

Commit

Permalink
fix(xrpc): support BodyInit as request body
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed May 15, 2024
1 parent 7575c17 commit ad66160
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CID } from 'multiformats/cid'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface CallOptions {
headers?: Headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CID } from 'multiformats/cid'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface OutputSchema {
blob: BlobRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface HandlerInput {
encoding: 'application/vnd.ipld.car'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface OutputSchema {
blob: BlobRef
Expand Down
4 changes: 2 additions & 2 deletions packages/lex-cli/src/codegen/lex-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ export function genXrpcInput(
)
}
} else if (def.type === 'procedure' && def.input?.encoding) {
//= export type InputSchema = string | Uint8Array
//= export type InputSchema = string | Uint8Array | Blob
file.addTypeAlias({
isExported: true,
name: 'InputSchema',
type: 'string | Uint8Array',
type: 'string | Uint8Array | Blob',
})
} else {
//= export type InputSchema = undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface HandlerInput {
encoding: 'application/vnd.ipld.car'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface OutputSchema {
blob: BlobRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface HandlerInput {
encoding: 'application/vnd.ipld.car'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { HandlerAuth, HandlerPipeThrough } from '@atproto/xrpc-server'

export interface QueryParams {}

export type InputSchema = string | Uint8Array
export type InputSchema = string | Uint8Array | Blob

export interface OutputSchema {
blob: BlobRef
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/tests/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ describe('crud operations', () => {
const result = await defaultFetchHandler(
aliceAgent.service.origin + `/xrpc/com.atproto.repo.createRecord`,
'post',
{ ...aliceAgent.api.xrpc.headers, 'Content-Type': 'application/json' },
{ ...aliceAgent.api.xrpc.headers, 'content-type': 'application/json' },
passthroughBody({
repo: alice.did,
collection: 'app.bsky.feed.post',
Expand Down
29 changes: 15 additions & 14 deletions packages/xrpc/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
encodeMethodCallBody,
httpResponseCodeToEnum,
httpResponseBodyParse,
normalizeHeaders,
} from './util'
import {
FetchHandler,
Expand Down Expand Up @@ -72,11 +71,11 @@ export class ServiceClient {
}

setHeader(key: string, value: string): void {
this.headers[key] = value
this.headers[key.toLowerCase()] = value
}

unsetHeader(key: string): void {
delete this.headers[key]
delete this.headers[key.toLowerCase()]
}

async call(
Expand All @@ -94,13 +93,13 @@ export class ServiceClient {

const httpMethod = getMethodSchemaHTTPMethod(def)
const httpUri = constructMethodCallUri(methodNsid, def, this.uri, params)
const httpHeaders = constructMethodCallHeaders(def, data, {
headers: {
...this.headers,
...opts?.headers,
},
encoding: opts?.encoding,
})
const httpHeaders = constructMethodCallHeaders(def, data, opts)

for (const [k, v] of Object.entries(this.headers)) {
if (v != null && !Object.hasOwn(httpHeaders, k)) {
httpHeaders[k] = v
}
}

const res = await this.baseClient.fetch(
httpUri,
Expand Down Expand Up @@ -145,11 +144,10 @@ export async function defaultFetchHandler(
try {
// The duplex field is now required for streaming bodies, but not yet reflected
// anywhere in docs or types. See whatwg/fetch#1438, nodejs/node#46221.
const headers = normalizeHeaders(httpHeaders)
const reqInit: RequestInit & { duplex: string } = {
method: httpMethod,
headers,
body: encodeMethodCallBody(headers, httpReqBody),
headers: httpHeaders,
body: encodeMethodCallBody(httpHeaders, httpReqBody),
duplex: 'half',
}
const res = await fetch(httpUri, reqInit)
Expand All @@ -160,7 +158,10 @@ export async function defaultFetchHandler(
body: httpResponseBodyParse(res.headers.get('content-type'), resBody),
}
} catch (e) {
throw new XRPCError(ResponseType.Unknown, String(e))
if (e instanceof XRPCError) throw e
const err = new XRPCError(ResponseType.Unknown, String(e))
err.cause = e
throw err
}
}

Expand Down
Loading

0 comments on commit ad66160

Please sign in to comment.