Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: initial #3

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
# fastify-plugin-example
# fastify-csv-import

Fastify plugin template.
A Fastify plugin that enables Fastify applications to parse CSV files and validate their content against a specified schema. It is built using `fast-csv` for parsing, `Ajv` for validation, and `fastify-multipart` for handling file uploads.

## Install

```sh
npm install @autotelic/fastify-csv-import
```

## Usage

```js
import Fastify from 'fastify'
import fastifyCsvImport from 'fastify-csv-import'

const PORT = 3000
const fastify = Fastify()

// Define your CSV validation schema
const validationSchema = {
type: 'object',
properties: {
'Catalog Title': { type: 'string' },
SKU: { type: 'string' },
'Fixed Price': { type: 'string', format: 'price' }
},
required: ['Catalog Title', 'SKU', 'Fixed Price']
}

fastify.register(fastifyCsvImport)

fastify.post('/csv/import', async (req, reply) => {
if (!fastify.csvImport) {
throw new Error('fastify-csv-import plugin is not available')
}
const { rows, errors } = await fastify.csvImport({ req, validationSchema })
reply.send(rows || errors)
})

fastify.listen({ port: PORT }, (_, address) => {
console.log(`Server listening at ${address}`)
})
```

## Example

For a more detailed example, including how to handle various scenarios and errors, please refer to our [usage example](./example/README.md).

## API

### fastify.csvImport({ req, validationSchema })

- `req`: The request object from Fastify.
- `validationSchema`: A schema object used for validating the CSV data. It must conform to the JSON Schema standard.

`fastify-csv-import` decorates fastify with `csvImport`. The function parses the CSV file from the request, validates each row against the provided schema, and returns an object containing `rows` and `errors`. Rows that pass validation are included in `rows`, while validation errors are recorded in `errors`.

### Configuration Options

- `MAX_CSV_IMPORT`: An optional environment variable to set the maximum CSV file size. If not set, defaults to 50MB.

## Triggering a Release

Expand All @@ -12,4 +70,4 @@ Trigger the release workflow via a tag
git checkout main && git pull
npm version { minor | major | path }
git push --follow-tags
```
```