npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@datocms/cda-client

v0.2.2

Published

JS client for DatoCMS GraphQL Content Delivery API based on the browser Fetch API

Downloads

3,222

Readme

👉 Visit the DatoCMS homepage or see What is DatoCMS?


@datocms/cda-client

A lightweight, TypeScript-ready package that offers various helpers around the native Fetch API to perform GraphQL requests towards DatoCMS Content Delivery API.

TypeScript Support

This package is built with TypeScript and provides type definitions out of the box. It supports TypedDocumentNode for improved type inference when using gql.tada, GraphQL Code Generator or similar tools.

Examples

Basic Query Execution

import { executeQuery } from '@datocms/cda-client';

const query = `
  query {
    allArticles {
      id
      title
    }
  }
`;

const result = await executeQuery(query, {
  token: 'your-api-token-here',
});

console.log(result);

Using with TypeScript and GraphQL Code Generator

import { executeQuery } from '@datocms/cda-client';
import { AllArticlesQuery } from './generated/graphql';

const result = await executeQuery(AllArticlesQuery, {
  token: 'your-api-token-here',
  variables: {
    limit: 10,
  },
});

console.log(result.allArticles);

Installation

npm install @datocms/cda-client

Usage

This package provides several utility functions to help you interact with the DatoCMS Content Delivery API using GraphQL.

executeQuery

The main function to execute a GraphQL query against the DatoCMS Content Delivery API.

import { executeQuery } from '@datocms/cda-client';

const result = await executeQuery(query, options);

Parameters

  • query: A GraphQL query string, DocumentNode, or TypedDocumentNode.
  • options: An object containing execution options.

Options

| Option | Type | Description | | -------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | | token | string | DatoCMS API token (required) Read more | | includeDrafts | boolean | If true, return draft versions of records Read more | | excludeInvalid | boolean | If true, filter out invalid records Read more | | environment | string | Name of the DatoCMS environment for the query Read more | | contentLink | 'vercel-v1' | If true, embed metadata for Content Link Read more | | baseEditingUrl | string | Base URL of your DatoCMS project Read more | | returnCacheTags | boolean | If true, receive Cache Tags associated with the query Read more | | variables | object | Variables to be sent with the query | | fetchFn | function | Custom fetch function (optional) | | requestInitOptions | Partial<RequestInit> | Additional request initialization options (optional) | | autoRetry | boolean | Automatically retry on rate limit (default: true) |

rawExecuteQuery

Similar to executeQuery, but returns both the query result and the full response object. This can be handy when used together with returnCacheTags to actually retrieve the cache tags.

import { rawExecuteQuery } from '@datocms/cda-client';

const [result, response] = await rawExecuteQuery(query, { token: 'your-api-token-here', returnCacheTags: true });
const cacheTags = response.headers.get('x-cache-tags');

buildRequestHeaders

Builds request headers for a GraphQL query towards the DatoCMS Content Delivery API.

import { buildRequestHeaders } from '@datocms/cda-client';

const headers = buildRequestHeaders(options);

Options

The buildRequestHeaders function accepts the same options as executeQuery, except for variables, fetchFn, and autoRetry.

buildRequestInit

Builds the request initialization object for a GraphQL query towards the DatoCMS Content Delivery API.

import { buildRequestInit } from '@datocms/cda-client';

const requestInit = buildRequestInit(query, options);

Parameters

  • query: A GraphQL query string or DocumentNode.
  • options: An object containing execution options (same as executeQuery).

Error Handling

In case a query fails (either with an HTTP status code outside of the 2xx range, or for an error in the query), an ApiError exception will be thrown by the client. This error contains all the details of the request and response, allowing you to debug and handle errors effectively.

Example

import { executeQuery, ApiError } from '@datocms/cda-client';

const query = `
  query {
    allArticles {
      id
      title
    }
  }
`;

try {
  const result = await executeQuery(query, {
    token: 'your-api-token-here',
  });
  console.log(result);
} catch (e) {
  if (e instanceof ApiError) {
    // Information about the failed request
    console.log(e.query);
    console.log(e.options);

    // Information about the response
    console.log(e.response.status);
    console.log(e.response.statusText);
    console.log(e.response.headers);
    console.log(e.response.body);
  } else {
    // Handle other types of errors
    throw e;
  }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.


What is DatoCMS?

DatoCMS is the REST & GraphQL Headless CMS for the modern web.

Trusted by over 25,000 enterprise businesses, agency partners, and individuals across the world, DatoCMS users create online content at scale from a central hub and distribute it via API. We ❤️ our developers, content editors and marketers!

Quick links:

Our featured repos:

Or see all our public repos