-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathhooks.ts
More file actions
175 lines (161 loc) · 7.1 KB
/
Copy pathhooks.ts
File metadata and controls
175 lines (161 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { errors, services } from "./protos";
import {
CustomerCredentials,
MutateOperation,
ReportOptions,
RequestOptions,
MutateOptions,
} from "./types";
export type BaseRequestHookArgs = {
credentials: CustomerCredentials;
query: string;
reportOptions?: ReportOptions;
};
export type BaseMutationHookArgs = {
credentials: CustomerCredentials;
method: `${keyof typeof services}.${string}`;
} & (
| // Mutation was executed with customer.mutateResources
{ mutations: MutateOperation<any>[]; isServiceCall: false }
// Mutation was executed with a service e.g. customer.campaigns.create
| { mutation: any; isServiceCall: true }
);
export type BaseServiceHookArgs = {
credentials: CustomerCredentials;
method: `${keyof typeof services}.${string}`;
requestOptions: any;
};
type StartHookArgs<T = RequestOptions | MutateOptions | any, A = void> = {
cancel: A extends void ? () => void : (args?: A) => void;
editOptions: (options: Partial<T>) => void;
};
type ErrorHookArgs = {
error: errors.GoogleAdsFailure | Error;
};
type EndHookArgs<
T = services.IGoogleAdsRow[] | services.MutateGoogleAdsResponse,
> = {
response?: T;
resolve: (args: any) => void;
};
type HookArgs = StartHookArgs | ErrorHookArgs | EndHookArgs;
type RequestHook<H extends HookArgs> = (a: BaseRequestHookArgs & H) => void;
type MutationHook<H extends HookArgs> = (a: BaseMutationHookArgs & H) => void;
type ServiceHook<H extends HookArgs> = (a: BaseServiceHookArgs & H) => void;
export type OnQueryStart = RequestHook<StartHookArgs<RequestOptions, any>>;
export type OnQueryError = RequestHook<ErrorHookArgs>;
export type OnQueryEnd = RequestHook<EndHookArgs<services.IGoogleAdsRow[]>>;
export type OnStreamStart = RequestHook<StartHookArgs<RequestOptions, void>>;
export type OnStreamError = RequestHook<ErrorHookArgs>;
export type OnMutationStart = MutationHook<StartHookArgs<MutateOptions, any>>;
export type OnMutationError = MutationHook<ErrorHookArgs>;
export type OnMutationEnd = MutationHook<
EndHookArgs<services.MutateGoogleAdsResponse>
>;
export type OnServiceStart = ServiceHook<StartHookArgs<any, any>>;
export type OnServiceError = ServiceHook<ErrorHookArgs>;
export type OnServiceEnd = ServiceHook<EndHookArgs<any>>;
export interface Hooks {
/**
* @description Hook called before execution of a query in the `query` and `report` methods
* @params `{ credentials, query, reportOptions, cancel, editOptions }`
* @param credentials customer id, login customer id, linked customer id
* @param query gaql
* @param reportOptions
* @param cancel utility function for cancelling the query. if an argument is provided then the query will return this argument
* @param editOptions utility function for editing the request options. any request option keys that are passed will be changed
*/
onQueryStart?: OnQueryStart;
/**
* @description Hook called upon a query throwing an error in the `query` and `report` methods
* @params `{ credentials, query, reportOptions, error }`
* @param credentials customer id, login customer id, linked customer id
* @param query gaql
* @param reportOptions
* @param error google ads error
*/
onQueryError?: OnQueryError;
/**
* @description Hook called after successful execution of a query in the `query` and `report` methods
* @params `{ credentials, query, reportOptions, response, resolve }`
* @param credentials customer id, login customer id, linked customer id
* @param query gaql
* @param reportOptions
* @param response results of the query, not available on reportStream
* @param resolve utility function for returning an alternative value from the query
*/
onQueryEnd?: OnQueryEnd;
/**
* @description Hook called before execution of a stream in the `reportStream` and `reportStreamRaw` methods
* @params `{ credentials, query, reportOptions, cancel, editOptions }`
* @param credentials customer id, login customer id, linked customer id
* @param query gaql
* @param reportOptions
* @param cancel utility function for cancelling the stream
* @param editOptions utility function for editing the request options. any request option keys that are passed will be changed
*/
onStreamStart?: OnStreamStart;
/**
* @description Hook called upon a stream throwing an error in the `reportStream` method. Will not be called for an error in `reportStreamRaw`
* @params `{ credentials, query, reportOptions, error }`
* @param credentials customer id, login customer id, linked customer id
* @param query gaql
* @param reportOptions
* @param error google ads error
*/
onStreamError?: OnStreamError;
/**
* @description Hook called before execution of a mutation
* @params `{ credentials, mutations, cancel, editOptions }`
* @param credentials customer id, login customer id, linked customer id
* @param mutations
* @param cancel utility function for cancelling the mutation. if an argument is provided then the query/report will return this argument
* @param editOptions utility function for editing the mutate options. any mutate option keys that are passed will be changed
*/
onMutationStart?: OnMutationStart;
/**
* @description Hook called upon a mutation throwing an error
* @params `{ credentials, mutations, error }`
* @param credentials customer id, login customer id, linked customer id
* @param mutations
* @param error google ads error
*/
onMutationError?: OnMutationError;
/**
* @description Hook called after successful execution of a mutation
* @params `{ credentials, mutations, response, resolve }`
* @param credentials customer id, login customer id, linked customer id
* @param mutations
* @param response results of the mutation
* @param resolve utility function for returning an alternative value from the mutation
*/
onMutationEnd?: OnMutationEnd;
/**
* @description Hook called before execution of a service
* @params `{ credentials, method, requestOptions }`
* @param credentials customer id, login customer id, linked customer id
* @param method
* @param cancel utility function for cancelling the service. if an argument is provided then the service will return this argument
* @param editOptions utility function for editing the service options. any service option keys that are passed will be changed
*/
onServiceStart?: OnServiceStart;
/**
* @description Hook called upon a service throwing an error
* @params `{ credentials, method, error }`
* @param credentials customer id, login customer id, linked customer id
* @param method
* @param error google ads error
*/
onServiceError?: OnServiceError;
/**
* @description Hook called after successful execution of a service
* @params `{ credentials, method, response, resolve }`
* @param credentials customer id, login customer id, linked customer id
* @param method
* @param response results of the service
* @param resolve utility function for returning an alternative value from the service
*/
onServiceEnd?: OnServiceEnd;
}
export type HookedCancellation = { cancelled: boolean; res?: any };
export type HookedResolution = { resolved: boolean; res?: any };