UNPKG

@peekalink/core

Version:
86 lines (61 loc) 1.8 kB
# `@peekalink/core` Core Peekalink API client for JavaScript. Can be used anywhere JavaScript runs. Fully type-safe and easy to use! ## Getting Started **1. Install the package:** ```sh pnpm install @peekalink/core ``` **2. Instantiate a Peekalink API client:** ```js import { PeekalinkClient } from "@peekalink/core"; const peekalink = new PeekalinkClient({ apiKey: "your-api-key", }); ``` **3. Preview a link:** ```js const preview = await peekalink.preview("https://youtu.be/QB7ACr7pUuE"); if (preview.type === "YOUTUBE_VIDEO") { console.log(preview.youtubeVideo); } ``` ## Customizing options You can either customize each request individually by passing an options object as the second argument to the `preview` method: ```js const preview = await peekalink.preview("https://youtu.be/QB7ACr7pUuE", { source: "WEB", }); ``` ...or you can set default options for all requests: ```js const peekalink = new PeekalinkClient({ apiKey: "your-api-key", defaultOptions: { source: "WEB", }, }); ``` See more about the [available options](https://docs.peekalink.io/api-reference/link-preview/get-a-link-preview#body-link). ## Customizing fetch options If you'd like to customize the fetch request options (e.g. headers, timeout, proxy, etc.), you can do so by passing a third parameter to the `preview` method: ```js const preview = await peekalink.preview("https://youtu.be/QB7ACr7pUuE", undefined, { // ... custom fetch options }); ``` ## Customizing the endpoint For things such as mocking unit tests, you can customize the endpoint that the client will make calls to by passing an `endpoint` option to the client's constructor: ```js const peekalink = new PeekalinkClient({ apiKey: "your-api-key", endpoint: "https://peekalink.test", }); ``` ## License MIT.