Skip to content

Commit

Permalink
[zapier] add response middleware (#19760)
Browse files Browse the repository at this point in the history
* feat: add zapier response middleware

* chore: add samples
  • Loading branch information
a-wallen authored Oct 2, 2024
1 parent 168a1d5 commit 43ce2a8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, '{{operationId}}', response.json);
return {{#returnType}}{{#returnTypeIsPrimitive}}{ data: results }{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}results{{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}results{{/returnType}};
})
},
Expand Down
26 changes: 19 additions & 7 deletions modules/openapi-generator/src/main/resources/zapier/utils.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ const isCreateAction = (key) => {
return !isSearchAction(key);
}

const requestOptionsMiddleware = (z, bundle, requestOptions) => {
// TODO: modify the request options for all outgoing request to your api
// if you are using session authentication without a Bearer token.
// This may be true if your API uses basic authentication or api keys.
return requestOptions
}

const isTrigger = (key) => {
// TODO: custom logic
return false
Expand All @@ -44,6 +37,24 @@ const triggerMiddleware = (action) => {
return action
}

const requestOptionsMiddleware = (z, bundle, requestOptions) => {
// TODO: modify the request options for all outgoing request to your api
// if you are using session authentication without a Bearer token.
// This may be true if your API uses basic authentication or api keys.
return requestOptions
}

const responseOptionsMiddleware = (z, bundle, key, json) => {
// TODO: modify if your response needs to be transformed before returning the
// data to Zapier. For example, you may need to map an id field to the
// "id" field. For example, map "contactId": 1 to "id": 1. Or wrap your
// response in a json object. For example, { data: response }.
//
// Note that if the type being returned from the endpoint is a primitive
// type, the response is automatically wrapped like this: { data: response }.
return json
}

module.exports = {
replacePathParameters: replacePathParameters,
childMapping: childMapping,
Expand All @@ -53,6 +64,7 @@ module.exports = {
isSearchAction: isSearchAction,
searchMiddleware: searchMiddleware,
requestOptionsMiddleware: requestOptionsMiddleware,
responseOptionsMiddleware: responseOptionsMiddleware,
isTrigger: isTrigger,
triggerMiddleware: triggerMiddleware,
isCreateAction: isCreateAction,
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/zapier/apis/PetApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'addPet', response.json);
return results;
})
},
Expand Down Expand Up @@ -84,7 +84,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'deletePet', response.json);
return results;
})
},
Expand Down Expand Up @@ -126,7 +126,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'findPetsByStatus', response.json);
return results;
})
},
Expand Down Expand Up @@ -168,7 +168,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'findPetsByTags', response.json);
return results;
})
},
Expand Down Expand Up @@ -211,7 +211,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'getPetById', response.json);
return results;
})
},
Expand Down Expand Up @@ -250,7 +250,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'updatePet', response.json);
return results;
})
},
Expand Down Expand Up @@ -302,7 +302,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'updatePetWithForm', response.json);
return results;
})
},
Expand Down Expand Up @@ -358,7 +358,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'uploadFile', response.json);
return results;
})
},
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/zapier/apis/StoreApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'deleteOrder', response.json);
return results;
})
},
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'getInventory', response.json);
return { data: results };
})
},
Expand Down Expand Up @@ -117,7 +117,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'getOrderById', response.json);
return results;
})
},
Expand Down Expand Up @@ -156,7 +156,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'placeOrder', response.json);
return results;
})
},
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/zapier/apis/UserApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'createUser', response.json);
return results;
})
},
Expand Down Expand Up @@ -76,7 +76,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'createUsersWithArrayInput', response.json);
return results;
})
},
Expand Down Expand Up @@ -118,7 +118,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'createUsersWithListInput', response.json);
return results;
})
},
Expand Down Expand Up @@ -160,7 +160,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'deleteUser', response.json);
return results;
})
},
Expand Down Expand Up @@ -203,7 +203,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'getUserByName', response.json);
return results;
})
},
Expand Down Expand Up @@ -253,7 +253,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'loginUser', response.json);
return { data: results };
})
},
Expand Down Expand Up @@ -289,7 +289,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'logoutUser', response.json);
return results;
})
},
Expand Down Expand Up @@ -333,7 +333,7 @@ module.exports = {
}
return z.request(utils.requestOptionsMiddleware(z, bundle, options)).then((response) => {
response.throwForStatus();
const results = response.json;
const results = utils.responseOptionsMiddleware(z, bundle, 'updateUser', response.json);
return results;
})
},
Expand Down
26 changes: 19 additions & 7 deletions samples/client/petstore/zapier/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ const isCreateAction = (key) => {
return !isSearchAction(key);
}

const requestOptionsMiddleware = (z, bundle, requestOptions) => {
// TODO: modify the request options for all outgoing request to your api
// if you are using session authentication without a Bearer token.
// This may be true if your API uses basic authentication or api keys.
return requestOptions
}

const isTrigger = (key) => {
// TODO: custom logic
return false
Expand All @@ -44,6 +37,24 @@ const triggerMiddleware = (action) => {
return action
}

const requestOptionsMiddleware = (z, bundle, requestOptions) => {
// TODO: modify the request options for all outgoing request to your api
// if you are using session authentication without a Bearer token.
// This may be true if your API uses basic authentication or api keys.
return requestOptions
}

const responseOptionsMiddleware = (z, bundle, key, json) => {
// TODO: modify if your response needs to be transformed before returning the
// data to Zapier. For example, you may need to map an id field to the
// "id" field. For example, map "contactId": 1 to "id": 1. Or wrap your
// response in a json object. For example, { data: response }.
//
// Note that if the type being returned from the endpoint is a primitive
// type, the response is automatically wrapped like this: { data: response }.
return json
}

module.exports = {
replacePathParameters: replacePathParameters,
childMapping: childMapping,
Expand All @@ -53,6 +64,7 @@ module.exports = {
isSearchAction: isSearchAction,
searchMiddleware: searchMiddleware,
requestOptionsMiddleware: requestOptionsMiddleware,
responseOptionsMiddleware: responseOptionsMiddleware,
isTrigger: isTrigger,
triggerMiddleware: triggerMiddleware,
isCreateAction: isCreateAction,
Expand Down

0 comments on commit 43ce2a8

Please sign in to comment.