本文整理汇总了TypeScript中caseless.default函数的典型用法代码示例。如果您正苦于以下问题:TypeScript default函数的具体用法?TypeScript default怎么用?TypeScript default使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了default函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: callback
}, function (err, res) {
if (err) return callback(err);
if (!res) return callback(new Error('Response should not be undefined if there is no error.'));
if (options.followRedirects && isRedirect(res.statusCode)) {
// prevent leakage of file handles
res.body.resume();
if (method === 'DELETE' && res.statusCode === 303) {
// 303 See Other should convert to GET for duplex
// requests and for DELETE
method = 'GET';
}
if (options.maxRedirects === 0) {
const err = new Error('Maximum number of redirects exceeded');
(err as any).res = res;
return callback(err, res);
}
options = {
...options,
duplex: false,
maxRedirects: options.maxRedirects && options.maxRedirects !== Infinity ? options.maxRedirects - 1 : options.maxRedirects,
};
// don't maintain headers through redirects
// This fixes a problem where a POST to http://example.com
// might result in a GET to http://example.co.uk that includes "content-length"
// as a header
const headers = caseless(options.headers);
const redirectHeaders: any = {};
if (options.allowRedirectHeaders) {
for (let i = 0; i < options.allowRedirectHeaders.length; i++) {
const headerName = options.allowRedirectHeaders[i];
const headerValue = headers.get(headerName);
if (headerValue) {
redirectHeaders[headerName] = headerValue;
}
}
}
options.headers = redirectHeaders;
const location = res.headers.location;
if (typeof location !== 'string') {
return callback(new Error('Cannot redirect to non string location: ' + location));
}
return request(duplex ? 'GET' : method, resolveUrl(url, location), options, callback);
} else {
return callback(null, res);
}
});
示例2: Caseless
import caseless, { Caseless, httpify } from "caseless";
new Caseless(); // $ExpectError
// tslint:disable-next-line: prefer-const
let c1: Caseless;
caseless(); // $ExpectType Caseless
caseless({}); // $ExpectType Caseless
caseless(null); // $ExpectError
caseless(1); // $ExpectError
caseless("2"); // $ExpectError
const c2 = caseless();
c2.set({}); // $ExpectType void
c2.set('foo', 'bar'); // $ExpectType string | false
c2.set('foo', 'bar', false); // $ExpectType string | false
c2.set('foo', new Date()); // $ExpectType string | false
c2.set(10, 'bar'); // $ExpectError
c2.get('foo'); // $ExpectType any
c2.get(10); // $ExpectError
c2.has('foo'); // $ExpectType string | false
c2.has(10); // $ExpectError
c2.swap('foo'); // $ExpectType void
c2.swap(10); // $ExpectError
c2.del('foo'); // $ExpectType boolean
示例3: toResponsePromise
return toResponsePromise(new Promise((resolve: (v: Response) => void, reject: (e: any) => void) => {
// check types of arguments
if (typeof method !== 'string') {
throw new TypeError('The method must be a string.');
}
if (typeof url !== 'string') {
throw new TypeError('The URL/path must be a string.');
}
if (options == null) {
options = {};
}
if (typeof options !== 'object') {
throw new TypeError('Options must be an object (or null).');
}
method = (method.toUpperCase() as any);
options.headers = options.headers || {};
var headers = caseless(options.headers);
// handle query string
if (options.qs) {
url = handleQs(url, options.qs);
}
const duplex = !(method === 'GET' || method === 'DELETE' || method === 'HEAD');
if (duplex) {
const body = handleBody(options);
body.getHeaders().then(bodyHeaders => {
Object.keys(bodyHeaders).forEach(key => {
if (!headers.has(key)) {
headers.set(key, bodyHeaders[key]);
}
});
ready(body);
}).catch(reject);
} else if (options.body) {
throw new Error(
'You cannot pass a body to a ' + method + ' request.'
);
} else {
ready();
}
function ready(body?: NormalizedBody) {
const req = basicRequest(method, url, {
allowRedirectHeaders: options.allowRedirectHeaders,
headers: options.headers,
followRedirects: options.followRedirects !== false,
maxRedirects: options.maxRedirects,
gzip: options.gzip !== false,
cache: options.cache,
agent: options.agent,
timeout: options.timeout,
socketTimeout: options.socketTimeout,
retry: options.retry,
retryDelay: options.retryDelay,
maxRetries: options.maxRetries,
isMatch: options.isMatch,
isExpired: options.isExpired,
canCache: options.canCache,
}, (err: NodeJS.ErrnoException | null, res?: GenericResponse<NodeJS.ReadableStream>) => {
if (err) return reject(err);
if (!res) return reject(new Error('No request was received'));
res.body.on('error', reject);
res.body.pipe(concat((body: Buffer) => {
resolve(
new GenericResponse(
res.statusCode,
res.headers,
Array.isArray(body) ? Buffer.alloc(0) : body,
res.url
)
);
}));
});
if (req && body) {
body.pipe(req);
}
}
}));
示例4: _request
function _request(method: HttpVerb, url: string, options: Options, callback: Callback): void | NodeJS.WritableStream {
const start = Date.now();
if (typeof method !== 'string') {
throw new TypeError('The method must be a string.');
}
if (typeof url !== 'string') {
throw new TypeError('The URL/path must be a string or a URL object.');
}
method = (method.toUpperCase() as any);
const urlObject = parseUrl(url);
const protocol = (urlObject.protocol || '').replace(/\:$/, '');
if (protocol !== 'http' && protocol !== 'https') {
throw new TypeError('The protocol "' + protocol + '" is not supported, cannot load "' + url + '"');
}
const rawHeaders = options.headers || {};
const headers = caseless(rawHeaders);
if (urlObject.auth) {
headers.set('Authorization', 'Basic ' + (Buffer.from(urlObject.auth)).toString('base64'));
}
const agent = 'agent' in options ? options.agent : false;
let cache = options.cache;
if (typeof cache === 'string') {
if (cache === 'file') {
cache = fileCache
} else if (cache === 'memory') {
cache = memoryCache;
}
}
if (cache && !(typeof cache === 'object' && typeof cache.getResponse === 'function' && typeof cache.setResponse === 'function' && typeof cache.invalidateResponse === 'function')) {
throw new TypeError(cache + ' is not a valid cache, caches must have `getResponse`, `setResponse` and `invalidateResponse` methods.');
}
const ignoreFailedInvalidation = options.ignoreFailedInvalidation;
if (options.duplex !== undefined && typeof options.duplex !== 'boolean') {
throw new Error('expected options.duplex to be a boolean if provided');
}
const duplex = options.duplex !== undefined ? options.duplex : !(method === 'GET' || method === 'DELETE' || method === 'HEAD');
const unsafe = !(method === 'GET' || method === 'OPTIONS' || method === 'HEAD');
if (options.gzip) {
headers.set('Accept-Encoding', headers.has('Accept-Encoding') ? headers.get('Accept-Encoding') + ',gzip,deflate' : 'gzip,deflate');
return _request(method, url, {
allowRedirectHeaders: options.allowRedirectHeaders,
duplex: duplex,
headers: rawHeaders,
agent: agent,
followRedirects: options.followRedirects,
retry: options.retry,
retryDelay: options.retryDelay,
maxRetries: options.maxRetries,
cache: cache,
timeout: options.timeout
}, function (err, res) {
if (err) return callback(err);
if (!res) return callback(new Error('Response should not be undefined if there is no error.'));
const newHeaders = ({...res.headers} as any);
let newBody = res.body;
switch (newHeaders['content-encoding']) {
case 'gzip':
delete newHeaders['content-encoding'];
newBody = res.body.pipe(createGunzip());
break;
case 'deflate':
delete newHeaders['content-encoding'];
newBody = res.body.pipe(createInflate());
break;
}
return callback(err, new Response(res.statusCode, newHeaders, newBody, res.url));
});
}
if (options.followRedirects) {
return _request(method, url, {
allowRedirectHeaders: options.allowRedirectHeaders,
duplex: duplex,
headers: rawHeaders,
agent: agent,
retry: options.retry,
retryDelay: options.retryDelay,
maxRetries: options.maxRetries,
cache: cache,
timeout: options.timeout
}, function (err, res) {
if (err) return callback(err);
if (!res) return callback(new Error('Response should not be undefined if there is no error.'));
if (options.followRedirects && isRedirect(res.statusCode)) {
// prevent leakage of file handles
res.body.resume();
if (method === 'DELETE' && res.statusCode === 303) {
// 303 See Other should convert to GET for duplex
// requests and for DELETE
method = 'GET';
}
if (options.maxRedirects === 0) {
const err = new Error('Maximum number of redirects exceeded');
(err as any).res = res;
//.........这里部分代码省略.........