Skip to content

Commit

Permalink
docs: write readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumball12 committed Feb 25, 2024
1 parent fecb396 commit 37f28df
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 2 deletions.
230 changes: 230 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,233 @@
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/google-spreadsheet-dts)](https://www.npmjs.com/package/google-spreadsheet-dts) ![[npm downloads]((https://img.shields.io/bundlephobia/minzip/google-spreadsheet-dts))](https://img.shields.io/npm/dm/google-spreadsheet-dts) ![GitHub package.json version (subfolder of monorepo)](https://img.shields.io/github/package-json/v/Gumball12/google-spreadsheet-dts?filename=package.json)

[![ci](https://github.com/Gumball12/google-spreadsheet-dts/actions/workflows/ci.yaml/badge.svg)](https://github.com/Gumball12/google-spreadsheet-dts/actions/workflows/ci.yaml) [![publish](https://github.com/Gumball12/google-spreadsheet-dts/actions/workflows/publish-npm.yaml/badge.svg)](https://github.com/Gumball12/google-spreadsheet-dts/actions/workflows/publish-npm.yaml) [![codecov](https://codecov.io/gh/Gumball12/google-spreadsheet-dts/graph/badge.svg?token=8uuKMCW2bk)](https://codecov.io/gh/Gumball12/google-spreadsheet-dts) [![changelog](https://img.shields.io/badge/CHANGELOG-gray)](./CHANGELOG.md)

![logo](./docs/logo-extended.png)

## 💫 Features

- [Parser presets](./src/parser) for public and private Google Sheets
- Generate types(`*.d.ts`) for Google Sheets at the desired location
- Customize the type and type file name

### TODO

- CLI (`npx google-spreadsheet-dts ...`)

## 📦 Install

```bash
npm i --save-dev google-spreadsheet-dts
yarn add -D google-spreadsheet-dts
pnpm add -D google-spreadsheet-dts
```

## 🚀 Usage

### 1. Select a Google Sheets parser preset

google-spreadsheet-dts provides parser presets for public and private Google Sheets. Check out the parser presets [here](./src/parser/README.md). You can also create custom parsers if needed.

### 2. Generate types with Google Sheets parser

Create the file scripts/generate-google-sheets-dts.ts. Here we use the parser preset [`publicGoogleSheetsParser`](./src/parser/publicGoogleSheetsParser.ts). You can check the sheet content [here](https://docs.google.com/spreadsheets/d/1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo/edit#gid=0).

```ts
// scripts/generate-google-sheets-dts.ts

import { generateDts } from 'google-spreadsheet-dts';
import { resolve } from 'node:path';
import { publicGoogleSheetsParser } from 'google-spreadsheet-dts/parser';

generateDts({
name: 'GoogleSheets',
directory: resolve(__dirname, '../src'),

createDts: {
// Define the types to import in the dts file
importTypes: [
{
name: 'StatusEnum',
from: './StatusEnum',
}
]
}

// Specify the Google Sheets to parse
parser: publicGoogleSheetsParser({
spreadSheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo',
path: ['Key', 'Property'],
typeName: 'Type',
publicGoogleSheetsParser: {
sheetName: 'ParserTest',
},
}),
});
```

Now, the types can be generated with the following command. The types are generated in the file src/GoogleSheets.ts.

```bash
ts-node scripts/generate-google-sheets-dts.ts
```

<details>
<summary>Using JavaScript</summary>

```js
// scripts/generate-google-sheets-dts.js

const { generateDts } = require('google-spreadsheet-dts');
const { resolve } = require('node:path');
const { publicGoogleSheetsParser } = require('google-spreadsheet-dts/parser');

generateDts({
name: 'GoogleSheets',
directory: resolve(__dirname, '../src'),

createDts: {
importTypes: [
{
name: 'StatusEnum',
from: './StatusEnum',
}
]
}

parser: publicGoogleSheetsParser({
spreadSheetId: '1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo',
path: ['Key', 'Property'],
typeName: 'Type',
publicGoogleSheetsParser: {
sheetName: 'ParserTest',
},
}),
});
```

```bash
node scripts/generate-google-sheets-dts.js
```

</details>

### 3. Use generated types

The above command generates the src/GoogleSheets.ts file as follows:

```ts
// src/GoogleSheets.ts

// Generated by google-spreadsheet-dts
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
import { StatusEnum } from './StatusEnum';
export {};
declare global {
export interface GoogleSheets {
click_conversation_data: {
conversation_id: string;
created_at: Date;
agent_type: string;
status: StatusEnum;
generate_position: 'conversation' | 'playground';
};
click_message_feedback_button: {
conversation_id: string;
message_id: string;
generated_position: 'conversation' | 'playground';
my_test: string | 'string';
};
}
}
```

The generated types are now available for use. Since the types are declared globally, you can use them without importing.

```ts
// src/my-event.ts

export const event = <T extends GoogleSheets, K extends keyof T>({
action,
properties,
}: {
action: K;
properties: Partial<T[K]>;
}) => {
// ...
};

event({
action: 'click_conversation_data',
properties: {
generate_position: 'conversation',
},
});
```

## 📚 API

### `generateDts`

```ts
function generateDts(params: GenerateDtsParams): Promise<void>;

type Params = {
name: string;
directory: string;
parser: Parser;
options?: GenerateDtsOptions;
};

type Parser = () => Promise<object> | object;

type GenerateDtsOptions = Partial<{
fileName: string;
output: (dts: string) => unknown;
createDts: CreateDtsOptions;
}>;
```

- `name`: Name of the type. If `options.fileName` is not specified, it will also be used as the file name.
- `directory`: Directory where the generated type file will be located.
- `parser`: A function that defines how to parse the Google Sheets. You can use [parser presets](./src/parser).
- `options`
- `fileName`: Name of the type file to generate. The default is the `name` parameter.
- `output`: A function that defines how to use the generated types. By default, it saves to a file.
- `createDts`: Options to customize the contents of the generated type file. See the [`createDts`](#createdts) section for details.

To create a custom parser, see the [Writing a custom parser](./src/parser/README.md#writing-a-custom-parser) section.

### `createDts`

```ts
function createDts(params: CreateDtsParams): string;

type CreateDtsParams = {
name: string;
object: object;
options?: CreateDtsOptions;
};

type CreateDtsOptions = Partial<{
defaultType: string;
importTypes: Import[];
}>;

type Import = {
name: string;
from: string;
};
```

- `name`: Name of the type.
- `object`: Content for the types.
- `options`
- `defaultType`: Type to use when the sheet type is an empty string, `undefined`, or `null`. Default is `any`.
- `importTypes`: Types to import from inside the file.

## License

[MIT](./LICENSE) @Gumball12
Binary file added docs/logo-extended.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ type GenerateDtsOptions = Partial<{
createDts: CreateDtsOptions;
}>;

type GenerateDtsParams = {
type Parser = () => Promise<object> | object;

export type GenerateDtsParams = {
name: string;
parser: () => Promise<object> | object;
parser: Parser;
directory: string;
options?: GenerateDtsOptions;
};
Expand Down
Loading

0 comments on commit 37f28df

Please sign in to comment.