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 +1 -1
- package/dist/index.cjs +9 -17
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -17
- package/package.json +1 -1
- package/src/types.ts +0 -10
- package/src/utils.ts +10 -21
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
|
|
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}
|
|
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
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
}));
|
package/dist/index.d.cts.map
CHANGED
|
@@ -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
|
|
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;;;;"}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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
|
|
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}
|
|
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
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
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}
|
|
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
|
|
32
|
+
throw new Error(
|
|
46
33
|
`Request to '${url}' failed with ${response.status}${
|
|
47
34
|
response.statusText ? `: ${response.statusText}` : ""
|
|
48
35
|
}`,
|
|
49
36
|
{
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
|
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
|
}));
|