Skip to content

Commit

Permalink
feat: types (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
meghein authored Jan 15, 2024
1 parent e9303ca commit 13dfb80
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 122 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
quote_type = single
24 changes: 24 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"root": true,
"extends": [
"@autotelic/eslint-config-js"
],
"settings": {
"node": {
"version": "^18.x"
}
},
"overrides": [{
"files": ["*.ts"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"]
}],
"rules": {
"multiline-comment-style": "off"
}
}
21 changes: 21 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Validate & Release
on:
push:
tags:
- v*
jobs:
validate:
uses: ./.github/workflows/validate.yaml

publish-npm:
runs-on: ubuntu-latest
needs: validate
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .node-version
registry-url: https://registry.npmjs.org/
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
37 changes: 0 additions & 37 deletions .github/workflows/release.yml

This file was deleted.

27 changes: 0 additions & 27 deletions .github/workflows/test.yml

This file was deleted.

19 changes: 19 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lint & Test

on:
workflow_call:
pull_request:
push:
branches: [main]

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .node-version
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm run validate
1 change: 0 additions & 1 deletion .husky/pre-commit

This file was deleted.

2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.15.1
18.19.0
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Stream CSVs from Fastify routes. Uses [fast-csv](https://c2fo.github.io/fast-csv

## Installation

```
```sh
npm i -S @autotelic/fastify-stream-to-csv
```

Expand Down Expand Up @@ -43,19 +43,8 @@ fastify.get('/report', async function (req, reply) {
## Github Actions/Workflows
#### Getting Started
* Create release and test workflows
```sh
cd .github/workflows
cp release.yml.example release.yml
cp test.yml.example test.yml
```
* Update `release.yml` and `test.yml` with appropriate workflow for your plugin
#### Triggering a Release
* Trigger the release workflow via release tag
```sh
git checkout main && git pull
npm version { minor | major | path }
Expand Down
9 changes: 4 additions & 5 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const { Readable } = require('stream')

const { fastifyStreamToCsv } = require('../../')

module.exports = async function (fastify, options) {
module.exports = async function csv (fastify, options) {
fastify.register(fastifyStreamToCsv)

fastify.get('/report', async function (req, reply) {
fastify.get('/report', async function report (req, reply) {
// create a readable stream
const readStream = Readable.from(Array.from(Array(100000).keys()))

// create a row formatter
const rowFormatter = num => {
return [`a${num}`, `b${num}`, `c${num}`]
}
const rowFormatter = num => [`a${num}`, `b${num}`, `c${num}`]

// these are fast-csv format options
const csvOptions = {
Expand Down
5 changes: 3 additions & 2 deletions examples/postgres-stream/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const pg = require('pg')
const QueryStream = require('pg-query-stream')

const { fastifyStreamToCsv } = require('../../')

const connectionString = 'postgres://postgres:[email protected]:5432/postgres?sslmode=disable'

module.exports = async function (fastify, options) {
module.exports = async function csv (fastify, options) {
fastify.register(fastifyStreamToCsv)

fastify.get('/db-report', async function (req, reply) {
fastify.get('/db-report', async function report (req, reply) {
// create a readable stream
const stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [10000])
const client = new pg.Client({ connectionString })
Expand Down
15 changes: 10 additions & 5 deletions examples/typesense-iterator/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable camelcase */
const { Readable } = require('stream')
const { fastifyStreamToCsv } = require('../../')

const Typesense = require('typesense')

const { fastifyStreamToCsv } = require('../../')

const client = new Typesense.Client({
nodes: [{
host: 'localhost',
Expand All @@ -14,9 +16,11 @@ const client = new Typesense.Client({
})

// async generator we can turn into a Readable
const searchPager = async function * () {
async function * searchPager () {
let total, shown
let page = 1; const per_page = 5
let page = 1
const per_page = 5

do {
const searchResults = await client.collections('books')
.documents()
Expand All @@ -34,17 +38,18 @@ const searchPager = async function * () {
total = found
shown = page * per_page
page = page + 1

// we want to yield each hit to be a csv row
for (const hit of hits) {
yield hit
}
} while (shown < total)
}

module.exports = async function (fastify, options) {
module.exports = async function csv (fastify, options) {
fastify.register(fastifyStreamToCsv)

fastify.get('/book-report', async function (req, reply) {
fastify.get('/book-report', async function report (req, reply) {
// create a readable stream from our search pager
const readStream = Readable.from(searchPager())

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fp = require('fastify-plugin')
const { format } = require('@fast-csv/format')
const fp = require('fastify-plugin')

async function replyDecorator (
readStream,
Expand Down
51 changes: 27 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
"version": "0.2.0",
"description": "Stream CSVs from Fastify routes",
"main": "index.js",
"directories": {
"test": "test"
"types": "types/index.d.ts",
"files": [
"index.js",
"types/index.d.ts"
],
"engines": {
"node": ">=18"
},
"scripts": {
"lint": "eslint .",
"test": "c8 --100 ava",
"lint": "standard",
"fix": "standard --fix",
"validate": "npm run lint && npm run test"
"test:types": "tsd",
"validate": "npm run lint && npm run test && npm run test:types"
},
"repository": {
"type": "git",
Expand All @@ -24,26 +29,24 @@
},
"homepage": "https://github.com/autotelic/fastify-stream-to-csv#readme",
"dependencies": {
"@fast-csv/format": "^4.3.5",
"fastify-plugin": "^3.0.0"
"@fast-csv/format": "^5.0.0",
"fastify-plugin": "^4.5.1"
},
"devDependencies": {
"ava": "^4.0.1",
"c8": "^7.10.0",
"csv-parse": "^5.0.4",
"fastify": "^4.10.2",
"fastify-cli": "^5.7.0",
"husky": "^8.0.2",
"lint-staged": "^13.1.0",
"pg": "^8.7.3",
"pg-query-stream": "^4.2.3",
"standard": "^17.0.0",
"supertest": "^6.2.2",
"typesense": "^1.1.3"
},
"lint-staged": {
"*.{js,jsx}": [
"npm run fix"
]
"@autotelic/eslint-config-js": "^0.2.1",
"@types/node": "^20.11.2",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"ava": "^6.0.1",
"c8": "^9.1.0",
"csv-parse": "^5.5.3",
"eslint": "^8.56.0",
"fastify": "^4.25.2",
"fastify-cli": "^6.0.1",
"pg": "^8.11.3",
"pg-query-stream": "^4.5.3",
"supertest": "^6.3.4",
"tsd": "^0.30.3",
"typesense": "^1.7.2"
}
}
13 changes: 7 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { Readable } = require('stream')

// eslint-disable-next-line import/no-unresolved
const test = require('ava')
// eslint-disable-next-line import/no-unresolved
const { parse } = require('csv-parse/sync')
const Fastify = require('fastify')
const supertest = require('supertest')
const { Readable } = require('stream')
const { parse } = require('csv-parse/sync')

const { fastifyStreamToCsv } = require('./index')

Expand All @@ -22,12 +25,10 @@ test('Should create a CSV file:', async (t) => {

fastify.register(fastifyStreamToCsv)

fastify.get('/', async function (req, reply) {
fastify.get('/', async function report (req, reply) {
const readStream = Readable.from(Array.from(Array(1).keys()))

const rowFormatter = num => {
return [`a${num}`, `b${num}`, `c${num}`]
}
const rowFormatter = num => [`a${num}`, `b${num}`, `c${num}`]

await reply.streamToCsv(readStream, rowFormatter, {
csvOptions: {
Expand Down
27 changes: 27 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Readable } from 'stream'

import { FormatterOptionsArgs } from '@fast-csv/format'
import type { FastifyPluginCallback } from 'fastify'

export interface StreamToCsvOptions {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
csvOptions?: FormatterOptionsArgs<any, any>
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type RowFormatterFunction = (row: any) => any[]

declare module 'fastify' {
interface FastifyReply {
streamToCsv(
readStream: Readable,
rowFormatter: RowFormatterFunction,
options?: StreamToCsvOptions
): void
}
}

declare const fastifyStreamToCsv: FastifyPluginCallback<StreamToCsvOptions>

export default fastifyStreamToCsv
export { fastifyStreamToCsv }
Loading

0 comments on commit 13dfb80

Please sign in to comment.