Skip to content

Commit

Permalink
docs: initial (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
meghein authored Jan 26, 2024
1 parent 2f9bf29 commit 7399c74
Showing 1 changed file with 61 additions and 3 deletions.
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
```
```

0 comments on commit 7399c74

Please sign in to comment.