google-sheets-mapper 2.1.0 → 2.2.0

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/README.md CHANGED
@@ -20,7 +20,7 @@ pnpm add google-sheets-mapper
20
20
  bun add google-sheets-mapper
21
21
  ```
22
22
 
23
- UMD build available on [unpkg](https://www.unpkg.com/browse/google-sheets-mapper@1.0.0/dist/google-sheets-mapper.cjs.production.min.js).
23
+ UMD build available on [unpkg](https://www.unpkg.com/browse/google-sheets-mapper/).
24
24
 
25
25
  ---
26
26
 
package/dist/index.cjs CHANGED
@@ -10,25 +10,17 @@ const getSheetsTitleUrl = (sheetId, apiKey)=>{
10
10
  };
11
11
  const getBatchUrl = (sheetId, ranges, apiKey)=>{
12
12
  const rangesQueryString = getRanges(ranges);
13
- return `${GOOGLE_API_URL}/${sheetId}?${rangesQueryString}&key=${apiKey}&includeGridData=true`;
13
+ return `${GOOGLE_API_URL}/${sheetId}/values:batchGet?${rangesQueryString}&key=${apiKey}`;
14
14
  };
15
- class ApiResponseError extends Error {
16
- constructor(message, response){
17
- super(message);
18
- Object.setPrototypeOf(this, ApiResponseError.prototype);
19
- this.response = response;
20
- if (Error.captureStackTrace) {
21
- Error.captureStackTrace(this, ApiResponseError);
22
- }
23
- }
24
- }
25
15
  const makeFetch = async (url, config = {})=>{
26
16
  const response = await fetch(url, config);
27
17
  if (!response.ok) {
28
- throw new ApiResponseError(`Request to '${url}' failed with ${response.status}${response.statusText ? `: ${response.statusText}` : ""}`, {
29
- status: response.status,
30
- statusText: response.statusText,
31
- url: response.url
18
+ throw new Error(`Request to '${url}' failed with ${response.status}${response.statusText ? `: ${response.statusText}` : ""}`, {
19
+ cause: {
20
+ status: response.status,
21
+ statusText: response.statusText,
22
+ url: response.url
23
+ }
32
24
  });
33
25
  }
34
26
  return await response.json();
@@ -42,7 +34,7 @@ const mapRecords = (records, headerData)=>{
42
34
  return obj;
43
35
  }, {}));
44
36
  };
45
- const mapData = ({ sheets, sheetsOptions = [] })=>{
37
+ const mapData = ({ sheets = [], sheetsOptions = [] })=>{
46
38
  return sheets.map((sheet)=>{
47
39
  const id = sheet.range.split("!")[0].replace(/'/g, "");
48
40
  const rows = sheet.values || [];
@@ -70,7 +62,7 @@ const fetchBatchData = async ({ apiKey, sheetId, sheetsOptions = [] })=>{
70
62
  };
71
63
  const fetchAllSheetsData = async ({ apiKey, sheetId })=>{
72
64
  const urlTitles = getSheetsTitleUrl(sheetId, apiKey);
73
- const { sheets } = await makeFetch(urlTitles);
65
+ const { sheets = [] } = await makeFetch(urlTitles);
74
66
  const sheetsOptions = sheets.map((sheet)=>({
75
67
  id: sheet.properties.title
76
68
  }));
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export interface SheetsOption {\n id: string;\n headerRowIndex?: number;\n}\n\nexport interface MapperOptions {\n apiKey: string;\n sheetId: string;\n sheetsOptions?: SheetsOption[];\n}\n\nexport interface ValueRange {\n majorDimensions: string;\n range: string;\n values: string[][];\n}\n\nexport interface ValueRangesResponse {\n spreadsheetId: string;\n valueRanges: ValueRange[];\n}\n\nexport interface PropertiesFromResponse {\n title: string;\n}\n\nexport interface SheetFromResponse {\n properties: PropertiesFromResponse;\n}\n\nexport interface SheetsResponse {\n sheets: SheetFromResponse[];\n}\n\nexport interface MapperState {\n id: string;\n data: Record<string, string>[];\n}\n\nexport interface ApiResponse {\n url: string;\n status: number;\n statusText: string;\n}\n\nexport interface ErrorResponse {\n response: ApiResponse;\n}\n","import type { MapperOptions, MapperState, ValueRangesResponse } from \"./types\";\nimport { fetchBatchData, fetchAllSheetsData, mapData } from \"./utils\";\n\nconst GoogleSheetsMapper = {\n async fetchGoogleSheetsData({\n apiKey,\n sheetId,\n sheetsOptions = [],\n }: MapperOptions): Promise<MapperState[]> {\n const response: ValueRangesResponse =\n sheetsOptions.length === 0\n ? await fetchAllSheetsData({ apiKey, sheetId })\n : await fetchBatchData({ apiKey, sheetId, sheetsOptions });\n return mapData({ sheets: response.valueRanges, sheetsOptions });\n },\n};\n\nexport default GoogleSheetsMapper;\nexport const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;\n"],"names":[],"mappings":"AAAO,UAAA,YAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA,oBAAA,YAAA;AACA;AAmBO,UAAA,WAAA;AACP;AACA,UAAA,MAAA;AACA;;AC7BA,cAAA,kBAAA;AACA,+DAAA,aAAA,GAAA,OAAA,CAAA,WAAA;AACA;;AAEO,cAAA,qBAAA,wCAAA,aAAA,KAAA,OAAA,CAAA,WAAA;;;;"}
1
+ {"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export interface SheetsOption {\n id: string;\n headerRowIndex?: number;\n}\n\nexport interface MapperOptions {\n apiKey: string;\n sheetId: string;\n sheetsOptions?: SheetsOption[];\n}\n\nexport interface ValueRange {\n majorDimensions: string;\n range: string;\n values: string[][];\n}\n\nexport interface ValueRangesResponse {\n spreadsheetId: string;\n valueRanges: ValueRange[];\n}\n\nexport interface PropertiesFromResponse {\n title: string;\n}\n\nexport interface SheetFromResponse {\n properties: PropertiesFromResponse;\n}\n\nexport interface SheetsResponse {\n sheets: SheetFromResponse[];\n}\n\nexport interface MapperState {\n id: string;\n data: Record<string, string>[];\n}\n","import type { MapperOptions, MapperState, ValueRangesResponse } from \"./types\";\nimport { fetchBatchData, fetchAllSheetsData, mapData } from \"./utils\";\n\nconst GoogleSheetsMapper = {\n async fetchGoogleSheetsData({\n apiKey,\n sheetId,\n sheetsOptions = [],\n }: MapperOptions): Promise<MapperState[]> {\n const response: ValueRangesResponse =\n sheetsOptions.length === 0\n ? await fetchAllSheetsData({ apiKey, sheetId })\n : await fetchBatchData({ apiKey, sheetId, sheetsOptions });\n return mapData({ sheets: response.valueRanges, sheetsOptions });\n },\n};\n\nexport default GoogleSheetsMapper;\nexport const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;\n"],"names":[],"mappings":"AAAO,UAAA,YAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA,oBAAA,YAAA;AACA;AAmBO,UAAA,WAAA;AACP;AACA,UAAA,MAAA;AACA;;AC7BA,cAAA,kBAAA;AACA,+DAAA,aAAA,GAAA,OAAA,CAAA,WAAA;AACA;;AAEO,cAAA,qBAAA,wCAAA,aAAA,KAAA,OAAA,CAAA,WAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export interface SheetsOption {\n id: string;\n headerRowIndex?: number;\n}\n\nexport interface MapperOptions {\n apiKey: string;\n sheetId: string;\n sheetsOptions?: SheetsOption[];\n}\n\nexport interface ValueRange {\n majorDimensions: string;\n range: string;\n values: string[][];\n}\n\nexport interface ValueRangesResponse {\n spreadsheetId: string;\n valueRanges: ValueRange[];\n}\n\nexport interface PropertiesFromResponse {\n title: string;\n}\n\nexport interface SheetFromResponse {\n properties: PropertiesFromResponse;\n}\n\nexport interface SheetsResponse {\n sheets: SheetFromResponse[];\n}\n\nexport interface MapperState {\n id: string;\n data: Record<string, string>[];\n}\n\nexport interface ApiResponse {\n url: string;\n status: number;\n statusText: string;\n}\n\nexport interface ErrorResponse {\n response: ApiResponse;\n}\n","import type { MapperOptions, MapperState, ValueRangesResponse } from \"./types\";\nimport { fetchBatchData, fetchAllSheetsData, mapData } from \"./utils\";\n\nconst GoogleSheetsMapper = {\n async fetchGoogleSheetsData({\n apiKey,\n sheetId,\n sheetsOptions = [],\n }: MapperOptions): Promise<MapperState[]> {\n const response: ValueRangesResponse =\n sheetsOptions.length === 0\n ? await fetchAllSheetsData({ apiKey, sheetId })\n : await fetchBatchData({ apiKey, sheetId, sheetsOptions });\n return mapData({ sheets: response.valueRanges, sheetsOptions });\n },\n};\n\nexport default GoogleSheetsMapper;\nexport const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;\n"],"names":[],"mappings":"AAAO,UAAA,YAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA,oBAAA,YAAA;AACA;AAmBO,UAAA,WAAA;AACP;AACA,UAAA,MAAA;AACA;;AC7BA,cAAA,kBAAA;AACA,+DAAA,aAAA,GAAA,OAAA,CAAA,WAAA;AACA;;AAEO,cAAA,qBAAA,wCAAA,aAAA,KAAA,OAAA,CAAA,WAAA;;;;"}
1
+ {"version":3,"file":"index.d.ts","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export interface SheetsOption {\n id: string;\n headerRowIndex?: number;\n}\n\nexport interface MapperOptions {\n apiKey: string;\n sheetId: string;\n sheetsOptions?: SheetsOption[];\n}\n\nexport interface ValueRange {\n majorDimensions: string;\n range: string;\n values: string[][];\n}\n\nexport interface ValueRangesResponse {\n spreadsheetId: string;\n valueRanges: ValueRange[];\n}\n\nexport interface PropertiesFromResponse {\n title: string;\n}\n\nexport interface SheetFromResponse {\n properties: PropertiesFromResponse;\n}\n\nexport interface SheetsResponse {\n sheets: SheetFromResponse[];\n}\n\nexport interface MapperState {\n id: string;\n data: Record<string, string>[];\n}\n","import type { MapperOptions, MapperState, ValueRangesResponse } from \"./types\";\nimport { fetchBatchData, fetchAllSheetsData, mapData } from \"./utils\";\n\nconst GoogleSheetsMapper = {\n async fetchGoogleSheetsData({\n apiKey,\n sheetId,\n sheetsOptions = [],\n }: MapperOptions): Promise<MapperState[]> {\n const response: ValueRangesResponse =\n sheetsOptions.length === 0\n ? await fetchAllSheetsData({ apiKey, sheetId })\n : await fetchBatchData({ apiKey, sheetId, sheetsOptions });\n return mapData({ sheets: response.valueRanges, sheetsOptions });\n },\n};\n\nexport default GoogleSheetsMapper;\nexport const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;\n"],"names":[],"mappings":"AAAO,UAAA,YAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA,oBAAA,YAAA;AACA;AAmBO,UAAA,WAAA;AACP;AACA,UAAA,MAAA;AACA;;AC7BA,cAAA,kBAAA;AACA,+DAAA,aAAA,GAAA,OAAA,CAAA,WAAA;AACA;;AAEO,cAAA,qBAAA,wCAAA,aAAA,KAAA,OAAA,CAAA,WAAA;;;;"}
package/dist/index.js CHANGED
@@ -8,25 +8,17 @@ const getSheetsTitleUrl = (sheetId, apiKey)=>{
8
8
  };
9
9
  const getBatchUrl = (sheetId, ranges, apiKey)=>{
10
10
  const rangesQueryString = getRanges(ranges);
11
- return `${GOOGLE_API_URL}/${sheetId}?${rangesQueryString}&key=${apiKey}&includeGridData=true`;
11
+ return `${GOOGLE_API_URL}/${sheetId}/values:batchGet?${rangesQueryString}&key=${apiKey}`;
12
12
  };
13
- class ApiResponseError extends Error {
14
- constructor(message, response){
15
- super(message);
16
- Object.setPrototypeOf(this, ApiResponseError.prototype);
17
- this.response = response;
18
- if (Error.captureStackTrace) {
19
- Error.captureStackTrace(this, ApiResponseError);
20
- }
21
- }
22
- }
23
13
  const makeFetch = async (url, config = {})=>{
24
14
  const response = await fetch(url, config);
25
15
  if (!response.ok) {
26
- throw new ApiResponseError(`Request to '${url}' failed with ${response.status}${response.statusText ? `: ${response.statusText}` : ""}`, {
27
- status: response.status,
28
- statusText: response.statusText,
29
- url: response.url
16
+ throw new Error(`Request to '${url}' failed with ${response.status}${response.statusText ? `: ${response.statusText}` : ""}`, {
17
+ cause: {
18
+ status: response.status,
19
+ statusText: response.statusText,
20
+ url: response.url
21
+ }
30
22
  });
31
23
  }
32
24
  return await response.json();
@@ -40,7 +32,7 @@ const mapRecords = (records, headerData)=>{
40
32
  return obj;
41
33
  }, {}));
42
34
  };
43
- const mapData = ({ sheets, sheetsOptions = [] })=>{
35
+ const mapData = ({ sheets = [], sheetsOptions = [] })=>{
44
36
  return sheets.map((sheet)=>{
45
37
  const id = sheet.range.split("!")[0].replace(/'/g, "");
46
38
  const rows = sheet.values || [];
@@ -68,7 +60,7 @@ const fetchBatchData = async ({ apiKey, sheetId, sheetsOptions = [] })=>{
68
60
  };
69
61
  const fetchAllSheetsData = async ({ apiKey, sheetId })=>{
70
62
  const urlTitles = getSheetsTitleUrl(sheetId, apiKey);
71
- const { sheets } = await makeFetch(urlTitles);
63
+ const { sheets = [] } = await makeFetch(urlTitles);
72
64
  const sheetsOptions = sheets.map((sheet)=>({
73
65
  id: sheet.properties.title
74
66
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "google-sheets-mapper",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "### A library for getting data from Google Sheets API v4",
5
5
  "keywords": [
6
6
  "google-sheets",
package/src/types.ts CHANGED
@@ -36,13 +36,3 @@ export interface MapperState {
36
36
  id: string;
37
37
  data: Record<string, string>[];
38
38
  }
39
-
40
- export interface ApiResponse {
41
- url: string;
42
- status: number;
43
- statusText: string;
44
- }
45
-
46
- export interface ErrorResponse {
47
- response: ApiResponse;
48
- }
package/src/utils.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type {
2
2
  MapperOptions,
3
3
  MapperState,
4
- ApiResponse,
5
4
  ValueRange,
6
5
  SheetsResponse,
7
6
  SheetFromResponse,
@@ -23,33 +22,23 @@ const getSheetsTitleUrl = (sheetId: string, apiKey: string): string => {
23
22
  const getBatchUrl = (sheetId: string, ranges: Array<string>, apiKey: string): string => {
24
23
  const rangesQueryString = getRanges(ranges);
25
24
 
26
- return `${GOOGLE_API_URL}/${sheetId}?${rangesQueryString}&key=${apiKey}&includeGridData=true`;
25
+ return `${GOOGLE_API_URL}/${sheetId}/values:batchGet?${rangesQueryString}&key=${apiKey}`;
27
26
  };
28
27
 
29
- class ApiResponseError extends Error {
30
- response: ApiResponse;
31
- constructor(message: string, response: ApiResponse) {
32
- super(message);
33
- Object.setPrototypeOf(this, ApiResponseError.prototype);
34
- this.response = response;
35
- if (Error.captureStackTrace) {
36
- Error.captureStackTrace(this, ApiResponseError);
37
- }
38
- }
39
- }
40
-
41
28
  const makeFetch = async <T>(url: string, config = {}): Promise<T> => {
42
29
  const response = await fetch(url, config);
43
30
 
44
31
  if (!response.ok) {
45
- throw new ApiResponseError(
32
+ throw new Error(
46
33
  `Request to '${url}' failed with ${response.status}${
47
34
  response.statusText ? `: ${response.statusText}` : ""
48
35
  }`,
49
36
  {
50
- status: response.status,
51
- statusText: response.statusText,
52
- url: response.url,
37
+ cause: {
38
+ status: response.status,
39
+ statusText: response.statusText,
40
+ url: response.url,
41
+ },
53
42
  },
54
43
  );
55
44
  }
@@ -75,10 +64,10 @@ const mapRecords = (
75
64
  };
76
65
 
77
66
  export const mapData = ({
78
- sheets,
67
+ sheets = [],
79
68
  sheetsOptions = [],
80
69
  }: {
81
- sheets: ValueRange[];
70
+ sheets?: ValueRange[];
82
71
  sheetsOptions?: SheetsOption[];
83
72
  }): MapperState[] => {
84
73
  return sheets.map((sheet: ValueRange) => {
@@ -121,7 +110,7 @@ export const fetchAllSheetsData = async ({
121
110
  sheetId,
122
111
  }: MapperOptions): Promise<ValueRangesResponse> => {
123
112
  const urlTitles = getSheetsTitleUrl(sheetId, apiKey);
124
- const { sheets }: SheetsResponse = await makeFetch(urlTitles);
113
+ const { sheets = [] }: SheetsResponse = await makeFetch(urlTitles);
125
114
  const sheetsOptions = sheets.map((sheet: SheetFromResponse) => ({
126
115
  id: sheet.properties.title,
127
116
  }));