web-csv-toolbox - v0.14.0
    Preparing search index...

    Function parseRequest

    • Parse HTTP Request what contains CSV to records, ideal for server-side use cases.

      Type Parameters

      Parameters

      • request: Request

        The request object to parse

      • Optionaloptions: Options

        Parsing options

      Returns AsyncIterableIterator<InferCSVRecord<Header, Options>>

      Async iterable iterator of records.

      If you want array of records, use parseRequest.toArray function.

      This function automatically treats request headers.

      • If Content-Type header is not set, it assumes text/csv.
      • If Content-Type header is not text/csv, it throws an error.
      • If Content-Type header has charset parameter, it uses it for decoding.
      • If Content-Encoding header is set, it decompresses the request.
      • Should there be any conflicting information between the header and the options, the option's value will take precedence.

      This function is particularly useful for server-side environments like Cloudflare Workers, Service Workers, or other edge computing platforms that use the Request API.

      import { parseRequest } from 'web-csv-toolbox';

      export default {
      async fetch(request: Request) {
      if (request.method === 'POST' && request.headers.get('content-type')?.includes('text/csv')) {
      for await (const record of parseRequest(request)) {
      console.log(record);
      }
      return new Response('CSV processed', { status: 200 });
      }
      return new Response('Not Found', { status: 404 });
      }
      };
      import { parseRequest } from 'web-csv-toolbox';

      self.addEventListener('fetch', (event) => {
      const request = event.request;
      if (request.method === 'POST' && request.url.endsWith('/upload-csv')) {
      event.respondWith(
      (async () => {
      const records = [];
      for await (const record of parseRequest(request)) {
      records.push(record);
      }
      return new Response(JSON.stringify(records), {
      headers: { 'Content-Type': 'application/json' }
      });
      })()
      );
      }
      });