@anmiles/google-api-wrapper 19.0.3 → 19.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [19.0.4](../../tags/v19.0.4) - 2025-06-02
9
+ ### Changed
10
+ - Fixed api property that was optional by mistake
11
+
8
12
  ## [19.0.3](../../tags/v19.0.3) - 2025-05-24
9
13
  ### Changed
10
14
  - Fixed path to templates
package/dist/lib/api.d.ts CHANGED
@@ -18,13 +18,13 @@ export interface CommonResponse<TItem> {
18
18
  nextPageToken?: string | null | undefined;
19
19
  }
20
20
  export declare class API<TGoogleAPI> {
21
+ api: TGoogleAPI;
22
+ private auth;
21
23
  private readonly getter;
22
24
  private readonly profile;
23
25
  private readonly authOptions?;
24
- api: TGoogleAPI | undefined;
25
- private auth;
26
- constructor(getter: (auth: GoogleApis.Common.OAuth2Client) => TGoogleAPI, profile: string, authOptions?: AuthOptions | undefined);
27
- init(): Promise<void>;
26
+ private constructor();
27
+ static init<TGoogleAPI>(getter: (auth: GoogleApis.Common.OAuth2Client) => TGoogleAPI, profile: string, authOptions?: AuthOptions): Promise<API<TGoogleAPI>>;
28
28
  getItems<TItem>(selectAPI: (api: TGoogleAPI) => CommonAPI<TItem>, params: object, options?: CommonOptions): Promise<TItem[]>;
29
29
  }
30
30
  export declare function getAPI<TGoogleAPI>(getter: (auth: GoogleApis.Common.OAuth2Client) => TGoogleAPI, profile: string, authOptions?: AuthOptions): Promise<API<TGoogleAPI>>;
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/lib/api.ts"],"names":[],"mappings":"AACA,OAAO,qBAAqB,CAAC;AAE7B,OAAO,KAAK,UAAU,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IAAI,CAAC;KACR,CAAC;CACF;AAED,MAAM,WAAW,cAAc,CAAC,KAAK;IACpC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC;IAChB,QAAQ,CAAC,EAAE;QACV,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;KACzC,CAAC;IACF,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;CAC1C;AAED,qBAAa,GAAG,CAAC,UAAU;IAElB,GAAG,EAAE,UAAU;IACtB,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;IAL9B,OAAO;WAQM,IAAI,CAAC,UAAU,EAC3B,MAAM,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,YAAY,KAAI,UAAU,EAC3D,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,WAAW,GACvB,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAKrB,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,CAAC,GAAG,EAAE,UAAU,KAAI,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;CAuCjI;AAED,wBAAsB,MAAM,CAAC,UAAU,EACtC,MAAM,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,YAAY,KAAI,UAAU,EAC3D,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,WAAW,GACvB,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAU1B"}
package/dist/lib/api.js CHANGED
@@ -12,19 +12,21 @@ const auth_1 = require("./auth");
12
12
  const credentials_1 = require("./credentials");
13
13
  const requestInterval = 300;
14
14
  class API {
15
+ api;
16
+ auth;
15
17
  getter;
16
18
  profile;
17
19
  authOptions;
18
- api;
19
- auth;
20
- constructor(getter, profile, authOptions) {
20
+ constructor(api, auth, getter, profile, authOptions) {
21
+ this.api = api;
22
+ this.auth = auth;
21
23
  this.getter = getter;
22
24
  this.profile = profile;
23
25
  this.authOptions = authOptions;
24
26
  }
25
- async init() {
26
- this.auth = await (0, auth_1.getAuth)(this.profile, this.authOptions);
27
- this.api = this.getter(this.auth);
27
+ static async init(getter, profile, authOptions) {
28
+ const { api, auth } = await resetAuth(getter, profile, authOptions);
29
+ return new API(api, auth, getter, profile, authOptions);
28
30
  }
29
31
  async getItems(selectAPI, params, options) {
30
32
  const items = [];
@@ -32,9 +34,6 @@ class API {
32
34
  do {
33
35
  let response;
34
36
  try {
35
- if (!this.api) {
36
- throw new Error('API is not initialized. Call `init` before getting items.');
37
- }
38
37
  const selectedAPI = selectAPI(this.api);
39
38
  response = await selectedAPI.list({ ...params, pageToken });
40
39
  }
@@ -43,7 +42,9 @@ class API {
43
42
  if ((message === 'invalid_grant' || message === 'Invalid credentials') && !this.authOptions?.temporary) {
44
43
  (0, logger_1.warn)('Access token stored is invalid, re-creating...');
45
44
  (0, credentials_1.deleteCredentials)(this.profile);
46
- await this.init();
45
+ const { api, auth } = await resetAuth(this.getter, this.profile, this.authOptions);
46
+ this.api = api;
47
+ this.auth = auth;
47
48
  return this.getItems(selectAPI, params, options);
48
49
  }
49
50
  else {
@@ -68,8 +69,11 @@ async function getAPI(getter, profile, authOptions) {
68
69
  (0, logger_1.warn)(`WARNING: trying to create permanent credentials using non-readonly scopes (${writableScopes.join(', ')}). Permanent credentials will be stored in the file and potentially might be re-used to modify your data`);
69
70
  }
70
71
  }
71
- const instance = new API(getter, profile, authOptions);
72
- await instance.init();
73
- return instance;
72
+ return API.init(getter, profile, authOptions);
73
+ }
74
+ async function resetAuth(getter, profile, authOptions) {
75
+ const auth = await (0, auth_1.getAuth)(profile, authOptions);
76
+ const api = getter(auth);
77
+ return { api, auth };
74
78
  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rapper around googleapis for getting data shortly",
5
5
  "keywords": [
6
6
  "google",
@@ -4,8 +4,8 @@ import type GoogleApis from 'googleapis';
4
4
  import type { calendar_v3 } from 'googleapis/build/src/apis/calendar';
5
5
  import { calendar } from 'googleapis/build/src/apis/calendar';
6
6
 
7
- import { API, getAPI } from '../api';
8
- import type { CommonResponse } from '../api';
7
+ import { getAPI } from '../api';
8
+ import type { API, CommonResponse } from '../api';
9
9
  import { getAuth } from '../auth';
10
10
  import { deleteCredentials } from '../credentials';
11
11
 
@@ -211,13 +211,6 @@ Permanent credentials will be stored in the file and potentially might be re-use
211
211
 
212
212
  expect(returnedItems).toEqual(items);
213
213
  });
214
-
215
- it('should throw if api was not initialized before getting items', async () => {
216
- instance = new API((auth) => calendar({ version: 'v3', auth }), profile);
217
-
218
- await expect(async () => instance.getItems(() => calendarList, params))
219
- .rejects.toEqual(new Error('API is not initialized. Call `init` before getting items.'));
220
- });
221
214
  });
222
215
  });
223
216
  });
package/src/lib/api.ts CHANGED
@@ -35,18 +35,21 @@ export interface CommonResponse<TItem> {
35
35
  }
36
36
 
37
37
  export class API<TGoogleAPI> {
38
- api: TGoogleAPI | undefined;
39
- private auth: GoogleApis.Common.OAuth2Client | undefined;
40
-
41
- constructor(
38
+ private constructor(
39
+ public api: TGoogleAPI,
40
+ private auth: GoogleApis.Common.OAuth2Client,
42
41
  private readonly getter: (auth: GoogleApis.Common.OAuth2Client)=> TGoogleAPI,
43
42
  private readonly profile: string,
44
43
  private readonly authOptions?: AuthOptions,
45
44
  ) { }
46
45
 
47
- async init(): Promise<void> {
48
- this.auth = await getAuth(this.profile, this.authOptions);
49
- this.api = this.getter(this.auth);
46
+ static async init<TGoogleAPI>(
47
+ getter: (auth: GoogleApis.Common.OAuth2Client)=> TGoogleAPI,
48
+ profile: string,
49
+ authOptions?: AuthOptions,
50
+ ): Promise<API<TGoogleAPI>> {
51
+ const { api, auth } = await resetAuth(getter, profile, authOptions);
52
+ return new API<TGoogleAPI>(api, auth, getter, profile, authOptions);
50
53
  }
51
54
 
52
55
  async getItems<TItem>(selectAPI: (api: TGoogleAPI)=> CommonAPI<TItem>, params: object, options?: CommonOptions): Promise<TItem[]> {
@@ -58,10 +61,6 @@ export class API<TGoogleAPI> {
58
61
  let response: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>>;
59
62
 
60
63
  try {
61
- if (!this.api) {
62
- throw new Error('API is not initialized. Call `init` before getting items.');
63
- }
64
-
65
64
  const selectedAPI = selectAPI(this.api);
66
65
 
67
66
  response = await selectedAPI.list({ ...params, pageToken });
@@ -71,7 +70,9 @@ export class API<TGoogleAPI> {
71
70
  if ((message === 'invalid_grant' || message === 'Invalid credentials') && !this.authOptions?.temporary) {
72
71
  warn('Access token stored is invalid, re-creating...');
73
72
  deleteCredentials(this.profile);
74
- await this.init();
73
+ const { api, auth } = await resetAuth(this.getter, this.profile, this.authOptions);
74
+ this.api = api;
75
+ this.auth = auth;
75
76
  return this.getItems(selectAPI, params, options);
76
77
  } else {
77
78
  throw ex;
@@ -105,7 +106,18 @@ export async function getAPI<TGoogleAPI>(
105
106
  }
106
107
  }
107
108
 
108
- const instance = new API<TGoogleAPI>(getter, profile, authOptions);
109
- await instance.init();
110
- return instance;
109
+ return API.init<TGoogleAPI>(getter, profile, authOptions);
110
+ }
111
+
112
+ async function resetAuth<TGoogleAPI>(
113
+ getter: (auth: GoogleApis.Common.OAuth2Client)=> TGoogleAPI,
114
+ profile: string,
115
+ authOptions?: AuthOptions,
116
+ ): Promise<{
117
+ api: TGoogleAPI;
118
+ readonly auth: GoogleApis.Common.OAuth2Client;
119
+ }> {
120
+ const auth = await getAuth(profile, authOptions);
121
+ const api = getter(auth);
122
+ return { api, auth };
111
123
  }