UNPKG

5.84 kBTypeScriptView Raw
1/**
2 * Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986.
3 * This class is a simple parser which creates the basic component parts
4 * (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation
5 * and encoding.
6 *
7 * ```txt
8 * foo://example.com:8042/over/there?name=ferret#nose
9 * \_/ \______________/\_________/ \_________/ \__/
10 * | | | | |
11 * scheme authority path query fragment
12 * | _____________________|__
13 * / \ / \
14 * urn:example:animal:ferret:nose
15 * ```
16 */
17export declare class URI implements UriComponents {
18 static isUri(thing: any): thing is URI;
19 /**
20 * scheme is the 'http' part of 'http://www.example.com/some/path?query#fragment'.
21 * The part before the first colon.
22 */
23 readonly scheme: string;
24 /**
25 * authority is the 'www.example.com' part of 'http://www.example.com/some/path?query#fragment'.
26 * The part between the first double slashes and the next slash.
27 */
28 readonly authority: string;
29 /**
30 * path is the '/some/path' part of 'http://www.example.com/some/path?query#fragment'.
31 */
32 readonly path: string;
33 /**
34 * query is the 'query' part of 'http://www.example.com/some/path?query#fragment'.
35 */
36 readonly query: string;
37 /**
38 * fragment is the 'fragment' part of 'http://www.example.com/some/path?query#fragment'.
39 */
40 readonly fragment: string;
41 /**
42 * @internal
43 */
44 protected constructor(scheme: string, authority?: string, path?: string, query?: string, fragment?: string, _strict?: boolean);
45 /**
46 * @internal
47 */
48 protected constructor(components: UriComponents);
49 /**
50 * Returns a string representing the corresponding file system path of this URI.
51 * Will handle UNC paths, normalizes windows drive letters to lower-case, and uses the
52 * platform specific path separator.
53 *
54 * * Will *not* validate the path for invalid characters and semantics.
55 * * Will *not* look at the scheme of this URI.
56 * * The result shall *not* be used for display purposes but for accessing a file on disk.
57 *
58 *
59 * The *difference* to `URI#path` is the use of the platform specific separator and the handling
60 * of UNC paths. See the below sample of a file-uri with an authority (UNC path).
61 *
62 * ```ts
63 const u = URI.parse('file://server/c$/folder/file.txt')
64 u.authority === 'server'
65 u.path === '/shares/c$/file.txt'
66 u.fsPath === '\\server\c$\folder\file.txt'
67 ```
68 *
69 * Using `URI#path` to read a file (using fs-apis) would not be enough because parts of the path,
70 * namely the server name, would be missing. Therefore `URI#fsPath` exists - it's sugar to ease working
71 * with URIs that represent files on disk (`file` scheme).
72 */
73 get fsPath(): string;
74 with(change: {
75 scheme?: string;
76 authority?: string | null;
77 path?: string | null;
78 query?: string | null;
79 fragment?: string | null;
80 }): URI;
81 /**
82 * Creates a new URI from a string, e.g. `http://www.example.com/some/path`,
83 * `file:///usr/home`, or `scheme:with/path`.
84 *
85 * @param value A string which represents an URI (see `URI#toString`).
86 */
87 static parse(value: string, _strict?: boolean): URI;
88 /**
89 * Creates a new URI from a file system path, e.g. `c:\my\files`,
90 * `/usr/home`, or `\\server\share\some\path`.
91 *
92 * The *difference* between `URI#parse` and `URI#file` is that the latter treats the argument
93 * as path, not as stringified-uri. E.g. `URI.file(path)` is **not the same as**
94 * `URI.parse('file://' + path)` because the path might contain characters that are
95 * interpreted (# and ?). See the following sample:
96 * ```ts
97 const good = URI.file('/coding/c#/project1');
98 good.scheme === 'file';
99 good.path === '/coding/c#/project1';
100 good.fragment === '';
101 const bad = URI.parse('file://' + '/coding/c#/project1');
102 bad.scheme === 'file';
103 bad.path === '/coding/c'; // path is now broken
104 bad.fragment === '/project1';
105 ```
106 *
107 * @param path A file system path (see `URI#fsPath`)
108 */
109 static file(path: string): URI;
110 static from(components: {
111 scheme: string;
112 authority?: string;
113 path?: string;
114 query?: string;
115 fragment?: string;
116 }): URI;
117 /**
118 * Creates a string representation for this URI. It's guaranteed that calling
119 * `URI.parse` with the result of this function creates an URI which is equal
120 * to this URI.
121 *
122 * * The result shall *not* be used for display purposes but for externalization or transport.
123 * * The result will be encoded using the percentage encoding and encoding happens mostly
124 * ignore the scheme-specific encoding rules.
125 *
126 * @param skipEncoding Do not encode the result, default is `false`
127 */
128 toString(skipEncoding?: boolean): string;
129 toJSON(): UriComponents;
130 static revive(data: UriComponents | URI): URI;
131 static revive(data: UriComponents | URI | undefined): URI | undefined;
132 static revive(data: UriComponents | URI | null): URI | null;
133 static revive(data: UriComponents | URI | undefined | null): URI | undefined | null;
134}
135export interface UriComponents {
136 scheme: string;
137 authority: string;
138 path: string;
139 query: string;
140 fragment: string;
141}
142/**
143 * Compute `fsPath` for the given uri
144 */
145export declare function uriToFsPath(uri: URI, keepDriveLetterCasing: boolean): string;
146/**
147 * Mapped-type that replaces all occurrences of URI with UriComponents
148 */
149export type UriDto<T> = {
150 [K in keyof T]: T[K] extends URI ? UriComponents : UriDto<T[K]>;
151};
152
\No newline at end of file