Files
omnivore/packages/import-handler/src/csv.ts
2023-05-24 18:09:42 +08:00

34 lines
1.1 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { parse } from '@fast-csv/parse'
import { Stream } from 'stream'
import { ImportContext } from '.'
export const importCsv = async (ctx: ImportContext, stream: Stream) => {
const parser = parse()
stream.pipe(parser)
for await (const row of parser) {
try {
const url = new URL(row[0])
const state = row.length > 1 && row[1] ? row[1] : undefined
// labels follows format: "[label1,label2]"
const labels =
row.length > 2
? (row[2] as string)
.slice(1, -1)
.split(',')
.map((l) => l.trim())
.filter((l) => l !== '')
: undefined
await ctx.urlHandler(ctx, url, state, labels)
ctx.countImported += 1
} catch (error) {
console.log('invalid url', row, error)
ctx.countFailed += 1
}
}
}