Skip to content

Commit

Permalink
Merge pull request DefinitelyTyped#32697 from wcarson/lil-uri
Browse files Browse the repository at this point in the history
[lil-uri] New definition
  • Loading branch information
PranavSenthilnathan authored Feb 13, 2019
2 parents 416f64e + 1d4e85d commit 4d53938
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 0 deletions.
214 changes: 214 additions & 0 deletions types/lil-uri/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Type definitions for lil-uri 0.2
// Project: https://github.com/lil-js/uri#readme
// Definitions by: Wayne Carson <https://github.com/wcarson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/**
* Global exposed when library is used outside of a module environment.
*/
export as namespace lil;

/**
* Exported function
*/
export = uri;

/**
* Parses the given URI string
*
* @param the URI string to parse.
*/
declare function uri(uri?: string): uri.URI;

declare namespace uri {
/**
* Provides access to URI component values and parser/builder functions.
*/
interface URI {
/**
* Parses the given URI string
* @param uri the URI to parse
*/
parse(uri: string): URIParts;

/**
* Builds URI components into a URI string
*/
build(): string;

/**
* Builds URI components into a URI string
*/
toString(): string;

/**
* Builds URI components into a URI string
*/
valueOf(): string;

/**
* Gets the protocol value
*/
protocol(): string;

/**
* Sets the protocol
* @param protocol the protocol
* @return this object
*/
protocol(protocol: string): URI;

/**
* Gets the host value
*/
host(): string;

/**
* Sets the host
* @param host the host
* @return this object
*/
host(host: string): URI;

/**
* Gets the hostname value
*/
hostname(): string;

/**
* Sets the hostname
* @param hostname the hostname
* @return this object
*/
hostname(hostname: string): URI;

/**
* Gets the port value
*/
port(): number;

/**
* Sets the port
* @param port the port
* @return this object
*/
port(port: string): URI;

/**
* Gets the auth value
*/
auth(): Credentials;

/**
* Sets the auth
* @param auth the auth
* @return this object
*/
auth(auth: string): URI;

/**
* Gets the user value
*/
user(): string;

/**
* Sets the user
* @param user the user
* @return this object
*/
user(user: string): URI;

/**
* Gets the password value
*/
password(): string;

/**
* Sets the password
* @param password the password
* @return this object
*/
password(password: string): URI;

/**
* Gets the path value
*/
path(): string;

/**
* Sets the path
* @param path the path
* @return this object
*/
path(path: string): URI;

/**
* Gets the search value
*/
search(): string;

/**
* Sets the search
* @param search the search
* @return this object
*/
search(search: string): URI;

/**
* Gets the query value
*/
query(): QueryString;

/**
* Sets the query
* @param query the query
* @return this object
*/
query(query: QueryString): URI;

/**
* Gets the hash value
*/
hash(): string;

/**
* Sets the hash
* @param hash the hash
* @return this object
*/
hash(hash: string): URI;
}

/**
* URI parts
*/
interface URIParts {
uri: string;
protocol: string;
host: string;
hostname: string;
port: string;
auth: string;
user: string;
password: string;
path: string;
search: string;
query: QueryString;
hash: string;
}

/**
* Map of query string keys and values
*/
interface QueryString {
[key: string]: string|string[];
}

/**
* Credentials map
*/
interface Credentials {
user: string;
password: string;
}
}
59 changes: 59 additions & 0 deletions types/lil-uri/lil-uri-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import uri = require('lil-uri');

//
// Tests are based on code samples on https://github.com/lil-js/uri#readme
//

//
// Parser
//

const url = uri('http://user:[email protected]:8080/bar/foo.xml?foo=bar&hello=world&#hash=1');

// $ExpectType string
url.protocol(); // -> http

// $ExpectType string
url.host(); // -> example.com:8080

// $ExpectType string
url.hostname(); // -> example.com

// $ExpectType number
url.port(); // -> 8080

// $ExpectType Credentials
url.auth(); // -> { user: 'user', password: 'pass' }

// $ExpectType string
url.user(); // -> user

// $ExpectType string
url.password(); // -> pass

// $ExpectType string
url.path(); // -> /bar/foo.xml

// $ExpectType string
url.search(); // -> foo=bar&hello=world

// $ExpectType QueryString
url.query(); // -> { foo: 'bar', hello: 'world' }

// $ExpectType string
url.hash(); // -> hash=1

//
// Builder
//

// $ExpectType string
uri()
.protocol('https')
.host('example.com')
.port('8080')
.auth('user:pass')
.path('/bar/foo.xml')
.query({ foo: 'bar', hello: 'world' })
.hash('hash=1')
.build(); // -> http://@example.com:8080/bar/foo.xml?foo=bar&hello=world&#frament=1
23 changes: 23 additions & 0 deletions types/lil-uri/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,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"lil-uri-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/lil-uri/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

0 comments on commit 4d53938

Please sign in to comment.