Skip to content

Commit

Permalink
Merge pull request #846 from zapier/IQQ-1578-middleware-types
Browse files Browse the repository at this point in the history
chore(types): Add `customOptions` field to z.request options
  • Loading branch information
eliangcs authored Aug 22, 2024
2 parents 6873028 + 98f628a commit edf9a48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/core/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
AfterResponseMiddleware,
BeforeRequestMiddleware,
Bundle,
PerformFunction,
ZObject,
} from './zapier.custom';
import type {
Expand Down Expand Up @@ -101,7 +100,10 @@ expectType<Trigger>(hookTrigger);

const searchOperation: BasicActionOperation = {
inputFields: [{ key: 'some-input-key-1', type: 'file', required: true }],
perform: async (z: ZObject, b: Bundle) => [{ data: true }],
perform: async (z: ZObject, b: Bundle) => {
z.request('https://example.com', { middlewareData: { resumable: true } });
return [{ data: true }];
},
};
expectType<BasicActionOperation>(searchOperation);

Expand All @@ -124,7 +126,12 @@ const addBearerHeader: BeforeRequestMiddleware = (request, z, bundle) => {
};
expectType<BeforeRequestMiddleware>(addBearerHeader);

const asyncBeforeRequest: BeforeRequestMiddleware = async (request) => request;
const asyncBeforeRequest: BeforeRequestMiddleware = async (request) => {
if (request.middlewareData?.resumable) {
// do something async etc.
}
return request;
};
expectType<BeforeRequestMiddleware>(asyncBeforeRequest);

const checkPermissionsError: AfterResponseMiddleware = (response, z) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/types/zapier.custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ export interface HttpRequestOptions {
timeout?: number;
url?: string;
skipThrowForStatus?: boolean;

/**
* This is a special field that can be used to pass data to
* middleware. It is not sent with the request, but is available in
* the `response` object that middleware receives. This is useful for
* things like `prefixErrorMessage` fields etc.
*/
middlewareData?: Record<string, any>;
}

interface BaseHttpResponse {
Expand Down

0 comments on commit edf9a48

Please sign in to comment.