Skip to content

Commit

Permalink
Add libdefs for qs (flow-typed#1541)
Browse files Browse the repository at this point in the history
* Add libdefs for qs

* Update qs folder name

* Update test
  • Loading branch information
102 authored and gantoine committed Nov 17, 2017
1 parent 54ed0dd commit a7cda84
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
55 changes: 55 additions & 0 deletions definitions/npm/qs_v6.5.x/flow_v0.45.x-/qs_v6.5.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
declare module "qs" {
declare type ParseOptions = {
allowPrototypes?: boolean,
arrayLimit?: number,
decoder?: Function,
delimiter?: string,
depth?: number,
parameterLimit?: number,
plainObjects?: boolean,
strictNullHandling?: boolean,
ignoreQueryPrefix?: boolean,
parseArrays?: boolean,
allowDots?: boolean
};

declare type ArrayFormat = "brackets" | "indices" | "repeat";

declare type FilterFunction = (prefix: string, value: any) => any;
declare type FilterArray = Array<string | number>;
declare type Filter = FilterArray | FilterFunction;

declare type StringifyOptions = {
encoder?: Function,
delimiter?: string,
strictNullHandling?: boolean,
skipNulls?: boolean,
encode?: boolean,
sort?: Function,
allowDots?: boolean,
serializeDate?: Function,
encodeValuesOnly?: boolean,
format?: string,
addQueryPrefix?: boolean,
arrayFormat?: ArrayFormat,
filter?: Filter
};

declare type Formatter = (any) => string;

declare type Formats = {
RFC1738: string,
RFC3986: string,
"default": string,
formatters: {
RFC1738: Formatter,
RFC3986: Formatter
}
};

declare module.exports: {
parse(str: string, opts?: ParseOptions): Object,
stringify(obj: Object | Array<any>, opts?: StringifyOptions): string,
formats: Formats
};
}
58 changes: 58 additions & 0 deletions definitions/npm/qs_v6.5.x/test_qs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// @flow

import { parse, stringify, formats } from "qs";

function noop() {}

parse("test");

// $ExpectError: should be a string
parse({});

parse("test");

parse("test", {
allowPrototypes: true,
arrayLimit: 5,
decoder: noop,
delimiter: "/",
depth: 10,
parameterLimit: 15,
plainObjects: true,
strictNullHandling: true,
ignoreQueryPrefix: true,
parseArrays: true,
allowDots: true
});

const obj = { test: null };

stringify(obj);

stringify([1, 2, "test"]);

stringify(obj, {
encoder: noop,
delimiter: "/",
strictNullHandling: true,
skipNulls: true,
encode: true,
sort: noop,
allowDots: true,
serializeDate: noop,
encodeValuesOnly: true,
format: "/",
addQueryPrefix: true
});

stringify(obj, { arrayFormat: "brackets" });
stringify(obj, { arrayFormat: "indices" });
stringify(obj, { arrayFormat: "repeat" });

// $ExpectError: arrayFormat is not an enum value
stringify(obj, { arrayFormat: false });
// $ExpectError: arrayFormat is not an enum value
stringify(obj, { arrayFormat: "test" });

// $ExpectError: should be an object
stringify("test");

0 comments on commit a7cda84

Please sign in to comment.