Skip to content

Commit 04dd101

Browse files
kritzwarewcoots
andauthored
release: upgrading Google Ads API to v10 (#364)
* release: upgrading Google Ads API to v10 * eslint formatting * added forced error type to service factory errors Co-authored-by: WillCooter <will@cooters.co.uk>
1 parent 967df31 commit 04dd101

10 files changed

Lines changed: 21237 additions & 22123 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</p>
88
<p align="center">
99
<a href="https://developers.google.com/google-ads/api/docs/release-notes">
10-
<img src="https://img.shields.io/badge/google%20ads-v9.1.0-009688.svg?style=flat-square">
10+
<img src="https://img.shields.io/badge/google%20ads-v10.0.0-009688.svg?style=flat-square">
1111
</a>
1212
<a href="https://www.npmjs.com/package/google-ads-api">
1313
<img src="https://img.shields.io/npm/v/google-ads-api.svg?style=flat-square">
@@ -102,7 +102,7 @@ const customer = client.Customer({
102102

103103
## List accessible customers
104104

105-
This is a special client method for listing the accessible customers for a given refresh token, and is equivalent to [CustomerService.listAccessibleCustomers](https://developers.google.com/google-ads/api/reference/rpc/v9/CustomerService#listaccessiblecustomers). It returns the resource names of available customer accounts.
105+
This is a special client method for listing the accessible customers for a given refresh token, and is equivalent to [CustomerService.listAccessibleCustomers](https://developers.google.com/google-ads/api/reference/rpc/v10/CustomerService#listaccessiblecustomers). It returns the resource names of available customer accounts.
106106

107107
```ts
108108
const client = new GoogleAdsApi({
@@ -566,9 +566,9 @@ const customer = client.Customer({
566566

567567
## Error handling
568568

569-
All errors, apart from GRPC specific cases (such as a connection problem or timeout, [see more here](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)), are instances of a [GoogleAdsFailure](https://developers.google.com/google-ads/api/reference/rpc/v9/GoogleAdsFailure).
569+
All errors, apart from GRPC specific cases (such as a connection problem or timeout, [see more here](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)), are instances of a [GoogleAdsFailure](https://developers.google.com/google-ads/api/reference/rpc/v10/GoogleAdsFailure).
570570

571-
You can find a list of all error types for a specific version in [the official documentation](https://developers.google.com/google-ads/api/reference/rpc/v9/AccessInvitationErrorEnum.AccessInvitationError), as well as more information about [handling errors here](https://developers.google.com/google-ads/api/docs/best-practices/error-types).
571+
You can find a list of all error types for a specific version in [the official documentation](https://developers.google.com/google-ads/api/reference/rpc/v10/AccessInvitationErrorEnum.AccessInvitationError), as well as more information about [handling errors here](https://developers.google.com/google-ads/api/docs/best-practices/error-types).
572572

573573
```ts
574574
import { errors } from "google-ads-api";

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"author": "Opteo",
1818
"license": "MIT",
1919
"dependencies": {
20-
"google-ads-node": "^7.0.0",
20+
"google-ads-node": "^8.0.0",
2121
"google-auth-library": "^7.1.0",
22-
"google-gax": "^2.28.1",
22+
"google-gax": "^2.29.7",
2323
"long": "^4.0.0"
2424
},
2525
"devDependencies": {

scripts/services.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ export default class ServiceFactory extends Service {
9090
for (const [methodName, methodDef] of Object.entries<MethodDefinition>(
9191
service.methods
9292
)) {
93-
if (methodName.startsWith("Get")) {
94-
const getMethod = compileGetMethod(methodName, methodDef);
95-
compiledMethods.push(getMethod);
96-
continue;
97-
}
9893
if (methodName.startsWith("Mutate")) {
9994
const { mutateMethods, mutateOptions } = compileMutateMethods(
10095
name,
@@ -154,10 +149,16 @@ function compileSpecialMethod(
154149
const requestType = `services.${methodDef.requestType}`;
155150

156151
// Some special methods use types such as google.longrunning.Operation
157-
const responseType = methodDef.responseType.includes("google.")
152+
let responseType = methodDef.responseType.includes("google.")
158153
? methodDef.responseType.split("google.")[1]
159154
: `services.${methodDef.responseType}`;
160155

156+
if (methodDef.responseType.includes(`.googleads.${googleAdsVersion}`)) {
157+
[, responseType] = methodDef.responseType.split(
158+
`.googleads.${googleAdsVersion}.`
159+
);
160+
}
161+
161162
return `
162163
/**
163164
* @link ${GOOGLE_ADS_DOCS_URL}/rpc/${VERSION}/${serviceName}#${methodName.toLowerCase()}
@@ -173,41 +174,7 @@ function compileSpecialMethod(
173174
});
174175
return response;
175176
} catch (err) {
176-
throw this.getGoogleAdsError(err);
177-
}
178-
}
179-
`;
180-
}
181-
182-
function compileGetMethod(
183-
methodName: string,
184-
methodDef: MethodDefinition
185-
): string {
186-
const serviceMethod = toCamelCase(methodName);
187-
const requestType = `services.${methodDef.requestType}`;
188-
const responseType = methodDef.responseType.split(`${VERSION}.`)[1];
189-
190-
return `
191-
/**
192-
* @description Retrieve a ${responseType} in full detail
193-
* @warning Don't use get in production!
194-
* @returns ${responseType}
195-
*/
196-
get: async (resourceName: string): Promise<${responseType}> => {
197-
const request = new ${requestType}({
198-
resource_name: resourceName,
199-
});
200-
try {
201-
// @ts-expect-error Response is an array type
202-
const [response] = await service.${serviceMethod}(request, {
203-
// @ts-expect-error This arg doesn't exist in the type definitions
204-
otherArgs: {
205-
headers: this.callHeaders,
206-
},
207-
});
208-
return response;
209-
} catch (err) {
210-
throw this.getGoogleAdsError(err);
177+
throw this.getGoogleAdsError(err as Error);
211178
}
212179
}
213180
`;
@@ -423,7 +390,7 @@ function buildMutateHookEnd(response: string) {
423390
}
424391

425392
function buildMutateHookError() {
426-
return `const googleAdsError = this.getGoogleAdsError(err);
393+
return `const googleAdsError = this.getGoogleAdsError(err as Error);
427394
if (this.hooks.onMutationError) {
428395
await this.hooks.onMutationError({
429396
...baseHookArguments,

0 commit comments

Comments
 (0)