From 323a951eb3735e1af9f4c0a632275edabe366408 Mon Sep 17 00:00:00 2001 From: Gumball12 Date: Thu, 29 Feb 2024 01:52:00 +0900 Subject: [PATCH] feat: make the instance be passed directly --- README.md | 19 ++++++------------- src/__tests__/parser.test.ts | 22 ++++++++-------------- src/parser/README.md | 21 +++++++-------------- src/parser/publicGoogleSheetsParser.ts | 20 ++++---------------- 4 files changed, 25 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 1b57bfb..81b6ed8 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,6 @@ This library automatically generates TypeScript types (`*.d.ts`) by parsing Goog - Generate types(`*.d.ts`) for Google Sheets at the desired location - Customize the type and type file name -### TODO - -- CLI (`npx google-spreadsheet-dts ...`) -- Private Google Sheets parser - ## 📦 Install ```bash @@ -44,6 +39,8 @@ import { generateDts } from 'google-spreadsheet-dts'; import { resolve } from 'node:path'; import { publicGoogleSheetsParser } from 'google-spreadsheet-dts/parser'; +import PublicGoogleSheetsParser from 'public-google-sheets-parser'; + generateDts({ name: 'GoogleSheets', directory: resolve(__dirname, '../src'), @@ -60,12 +57,9 @@ generateDts({ // Specify the Google Sheets to parse parser: publicGoogleSheetsParser({ - spreadSheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo', + instance: new PublicGoogleSheetsParser(/* ... */), path: ['Key', 'Property'], typeName: 'Type', - publicGoogleSheetsParser: { - sheetName: 'ParserTest', - }, }), }); ``` @@ -86,6 +80,8 @@ const { generateDts } = require('google-spreadsheet-dts'); const { resolve } = require('node:path'); const { publicGoogleSheetsParser } = require('google-spreadsheet-dts/parser'); +const PublicGoogleSheetsParser = require('public-google-sheets-parser'); + generateDts({ name: 'GoogleSheets', directory: resolve(__dirname, '../src'), @@ -100,12 +96,9 @@ generateDts({ } parser: publicGoogleSheetsParser({ - spreadSheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo', + instance: new PublicGoogleSheetsParser(/* ... */), path: ['Key', 'Property'], typeName: 'Type', - publicGoogleSheetsParser: { - sheetName: 'ParserTest', - }, }), }); ``` diff --git a/src/__tests__/parser.test.ts b/src/__tests__/parser.test.ts index c74324f..696ddbe 100644 --- a/src/__tests__/parser.test.ts +++ b/src/__tests__/parser.test.ts @@ -1,12 +1,16 @@ import { describe, it, expect } from 'vitest'; import { publicGoogleSheetsParser } from '../parser'; - -const SPREADSHEET_ID = '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo'; +import PublicGoogleSheetsParser from 'public-google-sheets-parser'; // https://docs.google.com/spreadsheets/d/1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo/edit#gid=0 const SHEET_NAME = 'ParserTest'; +const SPREADSHEET_ID = '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo'; describe('publicGoogleSheetsParser', () => { + const instance = new PublicGoogleSheetsParser(SPREADSHEET_ID, { + sheetName: SHEET_NAME, + }); + const expected = { click_conversation_data: { conversation_id: 'string', @@ -25,14 +29,9 @@ describe('publicGoogleSheetsParser', () => { it('Common forms', async () => { const parsed = await publicGoogleSheetsParser({ - spreadsheetId: SPREADSHEET_ID, + instance, path: ['Key', 'Property'], typeName: 'Type', - options: { - publicGoogleSheetsParser: { - sheetName: SHEET_NAME, - }, - }, })(); expect(parsed).toEqual(expected); @@ -40,14 +39,9 @@ describe('publicGoogleSheetsParser', () => { it('With empty lines', async () => { const parsed = await publicGoogleSheetsParser({ - spreadsheetId: SPREADSHEET_ID, + instance, path: ['Key', 'Property'], typeName: 'Type', - options: { - publicGoogleSheetsParser: { - sheetName: `${SHEET_NAME}-EmptyLines`, - }, - }, })(); expect(parsed).toEqual(expected); diff --git a/src/parser/README.md b/src/parser/README.md index 0f8a6b6..9234d37 100644 --- a/src/parser/README.md +++ b/src/parser/README.md @@ -5,7 +5,7 @@ ## public-google-sheets-parser -A parser that parses public Google Sheets. This parser uses [public-google-sheets-parser](https://github.com/fureweb-com/public-google-sheets-parser). +A parser that parses **public** Google Sheets. This parser uses [public-google-sheets-parser](https://github.com/fureweb-com/public-google-sheets-parser). ### Usage @@ -13,13 +13,13 @@ A parser that parses public Google Sheets. This parser uses [public-google-sheet import { generateDts } from 'google-spreadsheet-dts'; import { publicGoogleSheetsParser } from 'google-spreadsheet-dts/parser'; +import PublicGoogleSheetsParser from 'public-google-sheets-parser'; + +const instance = new PublicGoogleSheetsParser(/* ... */); const parser = publicGoogleSheetsParser({ - spreadSheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo', + instance, path: ['Key', 'Property'], typeName: 'Type', - publicGoogleSheetsParser: { - sheetName: 'ParserTest', - }, }); generateDts({ @@ -35,22 +35,15 @@ generateDts({ ```ts function publicGoogleSheetsParser(options: { - spreadSheetId: string; + instance: PublicGoogleSheetsParser; path: string[]; typeName: string; - publicGoogleSheetsParser: PublicGoogleSheetsParserOptions; }): Parser; - -type PublicGoogleSheetsParserOptions = { - sheetName: string; -}; ``` -- `spreadSheetId`: Google Sheets ID +- `instance`: An instance of [`PublicGoogleSheetsParser`](https://github.com/fureweb-com/public-google-sheets-parser?tab=readme-ov-file#usage-example) - `path`: List of column names where object property names exists - `typeName`: Column name where the type name exists -- `publicGoogleSheets` - - `sheetName`: Google Sheets sheet name For example, given the following Google Sheets: diff --git a/src/parser/publicGoogleSheetsParser.ts b/src/parser/publicGoogleSheetsParser.ts index a5c9e74..6a045e5 100644 --- a/src/parser/publicGoogleSheetsParser.ts +++ b/src/parser/publicGoogleSheetsParser.ts @@ -1,30 +1,18 @@ // Parser for public-google-sheets-parser // https://github.com/fureweb-com/public-google-sheets-parser -import PublicGoogleSheetsParser from 'public-google-sheets-parser'; - -type SheetName = string; -type PublicGoogleSheetsParserOptions = { sheetName: SheetName }; - -type Options = Partial<{ - publicGoogleSheetsParser: PublicGoogleSheetsParserOptions; -}>; +import type PublicGoogleSheetsParser from 'public-google-sheets-parser'; type Params = { - spreadsheetId: string; + instance: PublicGoogleSheetsParser; path: string[]; typeName: string; - options?: Options; }; export const publicGoogleSheetsParser = - ({ spreadsheetId, path, typeName, options }: Params) => + ({ instance, path, typeName }: Params) => async (): Promise => { - const parser = new PublicGoogleSheetsParser( - spreadsheetId, - options?.publicGoogleSheetsParser, - ); - const data = await parser.parse(); + const data = await instance.parse(); const filledData = data.map((item, index, data) => { if (index === 0) {