• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript caseless.default函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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);
   }
 });
开发者ID:ForbesLindesay,项目名称:http-basic,代码行数:46,代码来源:index.ts


示例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
开发者ID:Batbold-Gansukh,项目名称:DefinitelyTyped,代码行数:31,代码来源:caseless-tests.ts


示例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);
      }
    }
  }));
开发者ID:then,项目名称:then-request,代码行数:82,代码来源:index.ts


示例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;
//.........这里部分代码省略.........
开发者ID:ForbesLindesay,项目名称:http-basic,代码行数:101,代码来源:index.ts



注:本文中的caseless.default函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript cassandra-driver.Client类代码示例发布时间:2022-05-25
下一篇:
TypeScript canvasjs.Chart类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap