Skip to content

Commit 2974953

Browse files
committed
Ensure TS defs are generated
1 parent 77a6e71 commit 2974953

21 files changed

+6969
-17081
lines changed

dist/node/index.d.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export * from './typedefs';
2+
import { LoadOptions } from './typedefs';
3+
import { Response as SuperAgentResponse } from 'superagent';
4+
/**
5+
* Loads a document at the provided location and returns a JavaScript object representation.
6+
*
7+
* @param {string} location - The location to the document
8+
* @param {module:path-loader.LoadOptions} [options] - The loader options
9+
*
10+
* @returns {Promise<*>} Always returns a promise even if there is a callback provided
11+
*
12+
* @example
13+
* // Example using Promises
14+
*
15+
* PathLoader
16+
* .load('./package.json')
17+
* .then(JSON.parse)
18+
* .then(function (document) {
19+
* console.log(document.name + ' (' + document.version + '): ' + document.description);
20+
* }, function (err) {
21+
* console.error(err.stack);
22+
* });
23+
*
24+
* @example
25+
* // Example using options.prepareRequest to provide authentication details for a remotely secure URL
26+
*
27+
* PathLoader
28+
* .load('https://api.github.com/repos/whitlockjc/path-loader', {
29+
* prepareRequest: function (req, callback) {
30+
* req.auth('my-username', 'my-password');
31+
* callback(undefined, req);
32+
* }
33+
* })
34+
* .then(JSON.parse)
35+
* .then(function (document) {
36+
* console.log(document.full_name + ': ' + document.description);
37+
* }, function (err) {
38+
* console.error(err.stack);
39+
* });
40+
*
41+
* @example
42+
* // Example loading a YAML file
43+
*
44+
* PathLoader
45+
* .load('/Users/not-you/projects/path-loader/.travis.yml')
46+
* .then(YAML.safeLoad)
47+
* .then(function (document) {
48+
* console.log('path-loader uses the', document.language, 'language.');
49+
* }, function (err) {
50+
* console.error(err.stack);
51+
* });
52+
*
53+
* @example
54+
* // Example loading a YAML file with options.processContent (Useful if you need information in the raw response)
55+
*
56+
* PathLoader
57+
* .load('/Users/not-you/projects/path-loader/.travis.yml', {
58+
* processContent: function (res, callback) {
59+
* callback(YAML.safeLoad(res.text));
60+
* }
61+
* })
62+
* .then(function (document) {
63+
* console.log('path-loader uses the', document.language, 'language.');
64+
* }, function (err) {
65+
* console.error(err.stack);
66+
* });
67+
*/
68+
declare type LoaderReturn<T> = [T] extends [never] ? (string | SuperAgentResponse) : T;
69+
export declare function load<T = never>(location: string, options?: LoadOptions<LoaderReturn<T>>): Promise<LoaderReturn<T>>;

dist/node/index.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"use strict";
2+
/*
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2015 Jeremy Whitlock
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
26+
if (k2 === undefined) k2 = k;
27+
var desc = Object.getOwnPropertyDescriptor(m, k);
28+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
29+
desc = { enumerable: true, get: function() { return m[k]; } };
30+
}
31+
Object.defineProperty(o, k2, desc);
32+
}) : (function(o, m, k, k2) {
33+
if (k2 === undefined) k2 = k;
34+
o[k2] = m[k];
35+
}));
36+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
37+
Object.defineProperty(o, "default", { enumerable: true, value: v });
38+
}) : function(o, v) {
39+
o["default"] = v;
40+
});
41+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
42+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
43+
};
44+
var __importStar = (this && this.__importStar) || function (mod) {
45+
if (mod && mod.__esModule) return mod;
46+
var result = {};
47+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
48+
__setModuleDefault(result, mod);
49+
return result;
50+
};
51+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
52+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
53+
return new (P || (P = Promise))(function (resolve, reject) {
54+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
55+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
56+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
57+
step((generator = generator.apply(thisArg, _arguments || [])).next());
58+
});
59+
};
60+
var __importDefault = (this && this.__importDefault) || function (mod) {
61+
return (mod && mod.__esModule) ? mod : { "default": mod };
62+
};
63+
Object.defineProperty(exports, "__esModule", { value: true });
64+
exports.load = void 0;
65+
__exportStar(require("./typedefs"), exports);
66+
const HttpLoader = __importStar(require("./loaders/http"));
67+
const FilerLoader = __importStar(require("./loaders/file"));
68+
const bluebird_1 = __importDefault(require("bluebird"));
69+
const lodash_1 = require("lodash");
70+
const supportedLoaders = {
71+
file: FilerLoader,
72+
http: HttpLoader,
73+
https: HttpLoader,
74+
};
75+
const defaultLoader = typeof window === 'object' ? supportedLoaders.http : supportedLoaders.file;
76+
function getScheme(location) {
77+
if (!(0, lodash_1.isUndefined)(location)) {
78+
location = !location.includes('://') ? '' : location.split('://')[0];
79+
}
80+
return location;
81+
}
82+
/**
83+
* Utility that provides a single API for loading the content of a path/URL.
84+
*
85+
* @module path-loader
86+
*/
87+
function getLoader(location) {
88+
const scheme = getScheme(location);
89+
let loader = supportedLoaders[scheme];
90+
if ((0, lodash_1.isUndefined)(loader)) {
91+
if ((0, lodash_1.isEmpty)(scheme)) {
92+
loader = defaultLoader;
93+
}
94+
else {
95+
throw new Error('Unsupported scheme: ' + scheme);
96+
}
97+
}
98+
return loader;
99+
}
100+
function validateArgs(location, opts = {}) {
101+
if (typeof location === 'undefined') {
102+
throw new TypeError('location is required');
103+
}
104+
else if (typeof location !== 'string') {
105+
throw new TypeError('location must be a string');
106+
}
107+
if (typeof opts !== 'object') {
108+
throw new TypeError('options must be an object');
109+
}
110+
else if (typeof opts.processContent !== 'undefined' &&
111+
typeof opts.processContent !== 'function') {
112+
throw new TypeError('options.processContent must be a function');
113+
}
114+
}
115+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
116+
function load(location, options = {}) {
117+
return __awaiter(this, void 0, void 0, function* () {
118+
// Validate arguments
119+
validateArgs(location, options);
120+
// Load the document from the provided location and process it
121+
const loader = getLoader(location);
122+
const promisifiedLoader = bluebird_1.default.promisify(loader.load);
123+
const data = yield promisifiedLoader(location, options);
124+
if (!options.processContent) {
125+
return data;
126+
}
127+
// For consistency between file and http, always send an object with a 'text' property containing the raw
128+
// string value being processed.
129+
const res = (0, lodash_1.isPlainObject)(data)
130+
? (data)
131+
: { text: data };
132+
// Pass the path being loaded
133+
res.location = location;
134+
const processContent = bluebird_1.default.promisify(options.processContent);
135+
return processContent(res);
136+
});
137+
}
138+
exports.load = load;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* The file loader is not supported in the browser.
3+
*
4+
* @throws {error} the file loader is not supported in the browser
5+
*/
6+
export declare function getBase(): void;
7+
/**
8+
* The file loader is not supported in the browser.
9+
*/
10+
export declare function load(...rest: any[]): void;

dist/node/loaders/file-browser.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
/*
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2015 Jeremy Whitlock
6+
* Copyright (c) 2022 Robert Kesterson
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
Object.defineProperty(exports, "__esModule", { value: true });
27+
exports.load = exports.getBase = void 0;
28+
const unsupportedError = new TypeError(`The 'file' scheme is not supported in the browser`);
29+
/**
30+
* The file loader is not supported in the browser.
31+
*
32+
* @throws {error} the file loader is not supported in the browser
33+
*/
34+
function getBase() {
35+
throw unsupportedError;
36+
}
37+
exports.getBase = getBase;
38+
/**
39+
* The file loader is not supported in the browser.
40+
*/
41+
// eslint-disable-next-line
42+
function load(...rest) {
43+
const fn = rest[rest.length - 1];
44+
if (typeof fn === 'function') {
45+
fn(unsupportedError);
46+
}
47+
else {
48+
throw unsupportedError;
49+
}
50+
}
51+
exports.load = load;

dist/node/loaders/file.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { LoadCallback, LoadOptions } from '../typedefs';
2+
/**
3+
* Loads a file from the filesystem.
4+
*
5+
* @param location - The filesystem location (If relative, location is relative to process.cwd()).
6+
* @param options - The loader options (Unused)
7+
* @param callback - The error-first callback
8+
*/
9+
export declare function load(location: string, options: LoadOptions, callback: LoadCallback): void;
10+
export declare function loadAsync(location: string, options: LoadOptions): Promise<string>;

dist/node/loaders/file.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"use strict";
2+
/*
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2015 Jeremy Whitlock
6+
* Copyright (c) 2022 Robert Kesterson
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28+
return new (P || (P = Promise))(function (resolve, reject) {
29+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32+
step((generator = generator.apply(thisArg, _arguments || [])).next());
33+
});
34+
};
35+
var __importDefault = (this && this.__importDefault) || function (mod) {
36+
return (mod && mod.__esModule) ? mod : { "default": mod };
37+
};
38+
Object.defineProperty(exports, "__esModule", { value: true });
39+
exports.loadAsync = exports.load = void 0;
40+
const fs_1 = __importDefault(require("fs"));
41+
const path_1 = __importDefault(require("path"));
42+
const lodash_1 = require("lodash");
43+
/**
44+
* Loads a file from the filesystem.
45+
*
46+
* @param location - The filesystem location (If relative, location is relative to process.cwd()).
47+
* @param options - The loader options (Unused)
48+
* @param callback - The error-first callback
49+
*/
50+
function load(location, options, callback) {
51+
loadAsync(location, options)
52+
.then((data) => {
53+
callback(null, data);
54+
})
55+
.catch((err) => callback(err));
56+
}
57+
exports.load = load;
58+
function loadAsync(location, options) {
59+
var _a;
60+
return __awaiter(this, void 0, void 0, function* () {
61+
console.log('Start');
62+
if (!(0, lodash_1.isUndefined)(options.encoding) && !(0, lodash_1.isString)(options.encoding)) {
63+
throw new TypeError(`options.encoding must be a string`);
64+
}
65+
console.log(`'Valid' ${options.encoding}`);
66+
// Strip the scheme portion of the URI
67+
if (location.startsWith('file://')) {
68+
// Handle URI
69+
location = location.substring(7);
70+
}
71+
if (path_1.default.resolve(location) !== path_1.default.normalize(location)) {
72+
console.log(`Resolve relative path ${location}`);
73+
// Handle relative paths
74+
location = path_1.default.resolve(process.cwd(), location);
75+
}
76+
const data = fs_1.default.readFileSync(location, {});
77+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
78+
return data.toString(((_a = options.encoding) !== null && _a !== void 0 ? _a : 'utf-8'));
79+
});
80+
}
81+
exports.loadAsync = loadAsync;

dist/node/loaders/http.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { LoadCallback, LoadOptions } from '../typedefs';
2+
/**
3+
* Loads a file from an http or https URL.
4+
*
5+
* @param location - The document URL (If relative, location is relative to window.location.origin).
6+
* @param options - The loader options
7+
* @param callback - The error-first callback
8+
*/
9+
export declare function load(location: string, options: LoadOptions, callback: LoadCallback): void;
10+
export declare function loadAsync(location: string, options: LoadOptions): Promise<string>;

0 commit comments

Comments
 (0)