Skip to content

Commit

Permalink
headers for api witness
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed May 7, 2024
1 parent aa2acc8 commit c4975bf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/payloadset/packages/api/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type ApiCallWitnessConfigSchema = typeof ApiCallWitnessConfigSchema

export type ApiCallWitnessConfigBase = WitnessConfig<{
accept?: 'application/json'
headers?: Record<string, string | undefined>
queries?: ApiCall['queries']
schema: ApiCallWitnessConfigSchema
timeout?: number
Expand Down
1 change: 1 addition & 0 deletions packages/payloadset/packages/api/src/Payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Verb = 'get' | 'post'
export type Queries = Record<string, string>

export interface ApiCallFields {
headers?: Record<string, string | undefined>
queries?: Queries
verb?: Verb
}
Expand Down
8 changes: 6 additions & 2 deletions packages/payloadset/packages/api/src/Witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export class ApiCallWitness<TParams extends ApiCallWitnessParams = ApiCallWitnes
throw new Error('Unable to determine uri. No uri/uriTemplate specified in either the call or config.')
}

getHeaders(headers?: Record<string, string | undefined>): Record<string, string | undefined> {
return { ...this.params.headers, ...this.config.headers, ...headers }
}

protected override async observeHandler(inPayloads: ApiCall[] = []): Promise<ApiCallResult[]> {
await this.started('throw')
try {
Expand Down Expand Up @@ -110,15 +114,15 @@ export class ApiCallWitness<TParams extends ApiCallWitnessParams = ApiCallWitnes
}
}

private async httpGet(url: string, call: Hash): Promise<ApiCallResult> {
private async httpGet(url: string, call: Hash, headers?: Record<string, string | undefined>): Promise<ApiCallResult> {
const result: ApiCallResult = {
call,
schema: ApiCallResultSchema,
}
try {
switch (this.accept) {
case 'application/json': {
const axios = new AxiosJson({ headers: { ...this.params.headers, Accept: 'application/json' }, timeout: this.timeout })
const axios = new AxiosJson({ headers: { ...this.getHeaders(headers), Accept: 'application/json' }, timeout: this.timeout })
const response = await axios.get<ApiCallJsonResultType>(url)
if (response.status >= 200 && response.status < 300) {
const jsonResult = result as ApiCallJsonResult
Expand Down

0 comments on commit c4975bf

Please sign in to comment.