Type alias KyInstance

KyInstance: {
    create: ((defaultOptions) => KyInstance);
    delete: ((url, options?) => ResponsePromise);
    extend: ((defaultOptions) => KyInstance);
    get: ((url, options?) => ResponsePromise);
    head: ((url, options?) => ResponsePromise);
    patch: ((url, options?) => ResponsePromise);
    post: ((url, options?) => ResponsePromise);
    put: ((url, options?) => ResponsePromise);
    stop: typeof stop;
    (url, options?): ResponsePromise;
}

Type declaration

    • (url, options?): ResponsePromise
    • Fetch the given url.

      Parameters

      • url: Input

        Request object, URL object, or URL string.

      • Optional options: Options

      Returns ResponsePromise

      A promise with Body method added.

      Example

      import ky from 'ky';

      const json = await ky('https://example.com', {json: {foo: true}}).json();

      console.log(json);
      //=> `{data: '🦄'}`
  • create: ((defaultOptions) => KyInstance)

    Create a new Ky instance with complete new defaults.

    Returns

    A new Ky instance.

  • delete: ((url, options?) => ResponsePromise)

    Fetch the given url using the option {method: 'delete'}.

    Returns

    A promise with Body methods added.

  • extend: ((defaultOptions) => KyInstance)

    Create a new Ky instance with some defaults overridden with your own.

    In contrast to ky.create(), ky.extend() inherits defaults from its parent.

    Returns

    A new Ky instance.

  • get: ((url, options?) => ResponsePromise)

    Fetch the given url using the option {method: 'get'}.

    Returns

    A promise with Body methods added.

  • head: ((url, options?) => ResponsePromise)

    Fetch the given url using the option {method: 'head'}.

    Returns

    A promise with Body methods added.

  • patch: ((url, options?) => ResponsePromise)

    Fetch the given url using the option {method: 'patch'}.

    Returns

    A promise with Body methods added.

  • post: ((url, options?) => ResponsePromise)

    Fetch the given url using the option {method: 'post'}.

    Returns

    A promise with Body methods added.

  • put: ((url, options?) => ResponsePromise)

    Fetch the given url using the option {method: 'put'}.

    Returns

    A promise with Body methods added.

  • Readonly stop: typeof stop

    A Symbol that can be returned by a beforeRetry hook to stop the retry. This will also short circuit the remaining beforeRetry hooks.

    Note: Returning this symbol makes Ky abort and return with an undefined response. Be sure to check for a response before accessing any properties on it or use optional chaining. It is also incompatible with body methods, such as .json() or .text(), because there is no response to parse. In general, we recommend throwing an error instead of returning this symbol, as that will cause Ky to abort and then throw, which avoids these limitations.

    A valid use-case for ky.stop is to prevent retries when making requests for side effects, where the returned data is not important. For example, logging client activity to the server.

    Example

    import ky from 'ky';

    const options = {
    hooks: {
    beforeRetry: [
    async ({request, options, error, retryCount}) => {
    const shouldStopRetry = await ky('https://example.com/api');
    if (shouldStopRetry) {
    return ky.stop;
    }
    }
    ]
    }
    };

    // Note that response will be `undefined` in case `ky.stop` is returned.
    const response = await ky.post('https://example.com', options);

    // Using `.text()` or other body methods is not supported.
    const text = await ky('https://example.com', options).text();

Generated using TypeDoc