Skip to content

Commit

Permalink
Add typings for proper-url-join (#39197)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr authored and andrewbranch committed Oct 17, 2019
1 parent 439a7d5 commit dca759d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
59 changes: 59 additions & 0 deletions types/proper-url-join/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Type definitions for proper-url-join 2.0
// Project: https://github.com/moxystudio/js-proper-url-join
// Definitions by: Jules Sam. Randolph <https://github.com/jsamr>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0

import { StringifyOptions } from 'query-string';

export interface Options {
/**
* Add a leading slash.
*
* **Default**: `true`
*/
leadingSlash?: boolean;
/**
* Add a trailing slash.
*
* **Default**: `false`
*/
trailingSlash?: boolean;
/**
* Protocol relative URLs.
*
* **Default**: `false`
*/
protocolRelative?: boolean;
/**
* Query string object that will be properly stringified and appended to the url.
* It will be merged with the query string in the url, if it exists.
*/
query?: {
[k: string]: string|number|ReadonlyArray<string|number>;
};
/**
* [query-string](https://github.com/sindresorhus/query-string#stringifyobject-options) singify method options to be considered when stringifying the query.
*/
queryOptions?: StringifyOptions;
}

export type PathArg = string|null|undefined|number;

interface urlJoin {
(p1: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, p6: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, p6: PathArg, p7: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, p6: PathArg, p7: PathArg, p8: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, p6: PathArg, p7: PathArg, p8: PathArg, p9: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, p6: PathArg, p7: PathArg, p8: PathArg, p9: PathArg, p10: PathArg, options?: Options): string;
(p1: PathArg, p2: PathArg, p3: PathArg, p4: PathArg, p5: PathArg, p6: PathArg, p7: PathArg, p8: PathArg, p9: PathArg, p10: PathArg, p11: PathArg, ...args: Array<PathArg|Options>): string;
}

declare const urlJoin: urlJoin;

export default urlJoin;
6 changes: 6 additions & 0 deletions types/proper-url-join/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"query-string": "^6.3.0"
}
}
31 changes: 31 additions & 0 deletions types/proper-url-join/proper-url-join-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import urlJoin from 'proper-url-join';

urlJoin('foo', 'bar'); // /foo/bar
urlJoin('/foo/', '/bar/'); // /foo/bar
urlJoin('foo', '', 'bar'); // /foo/bar
urlJoin('foo', undefined, 'bar'); // /foo/bar
urlJoin('foo', null, 'bar'); // /foo/bar

// With leading & trailing slash options
urlJoin('foo', 'bar', { leadingSlash: false }); // foo/bar
urlJoin('foo', 'bar', { trailingSlash: true }); // /foo/bar/
urlJoin('foo', 'bar', { leadingSlash: false, trailingSlash: true }); // foo/bar/

// Absolute URLs
urlJoin('http://google.com', 'foo'); // http://google.com/foo

// Protocol relative URLs
urlJoin('//google.com', 'foo', { protocolRelative: true }); // //google.com/foo

// With query string as an url part
urlJoin('foo', 'bar?queryString'); // /foo/bar?queryString
urlJoin('foo', 'bar?queryString', { trailingSlash: true }); // /foo/bar/?queryString

// With query string as an object
urlJoin('foo', { query: { biz: 'buz', foo: 'bar' } }); // /foo?biz=buz&foo=bar

// With both query string as an url part and an object
urlJoin('foo', 'bar?queryString', { query: { biz: 'buz', foo: 'bar' } });

// $ExpectError
urlJoin('foo', 'bar?queryString', { query: { biz: 'buz', foo: 'bar' } }, 'wrong');
23 changes: 23 additions & 0 deletions types/proper-url-join/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"proper-url-join-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/proper-url-join/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

0 comments on commit dca759d

Please sign in to comment.