Effect CommunityEC
Effect Communityโ€ข3y agoโ€ข
19 replies
shaugh

Challenges with Parsing Query Strings and 3rd Party Type from 'qs'

I'm trying to write a schema for parsing query strings, and I'm getting stuck dealing with the 3rd party type from qs:

import qs from "qs";
import * as Schema from "@effect/schema/Schema";

const ParsedQs: Schema.Schema<qs.ParsedQs> = Schema.lazy(() =>
  Schema.record(
    Schema.string,
    Schema.union(
      Schema.undefined,
      Schema.string,
      Schema.array(Schema.string),
      ParsedQs,
      Schema.array(ParsedQs)
    )
  )
);

const parsedQsFromString = Schema.transform(
  Schema.string,
  ParsedQs,
  (input) => qs.parse(input),
  (output) => qs.stringify(output)
);

export const fromQueryStringParameters = <A>(schema: Schema.Schema<any, A>) =>
  Schema.struct({
    rawQueryString: Schema.compose(parsedQsFromString, schema),
  });


gives the following error:

error TS2322: Type 'Schema<{ readonly [x: string]: string | readonly string[] | ParsedQs | readonly ParsedQs[] | undefined; }, { readonly [x: string]: string | readonly string[] | ParsedQs | readonly ParsedQs[] | undefined; }>' is not assignable to type 'Schema<ParsedQs, ParsedQs>'.
  The types returned by 'From(...)' are incompatible between these types.
    Type '{ readonly [x: string]: string | readonly string[] | ParsedQs | readonly ParsedQs[] | undefined; }' is not assignable to type 'ParsedQs'.

65 const ParsedQs: Schema.Schema<qs.ParsedQs> = Schema.lazy(() =>
         ~~~~~~~~


Is there maybe a better way to do this, or to work around the problem of qs.ParsedQs properties not being
readonly
? ๐Ÿ˜ฎโ€๐Ÿ’จ
Was this page helpful?