2.0.0 • Published 4 months ago

@grace-studio/next-strapi v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@grace-studio/next-strapi

npm version

Middle layer to connect a Next.js application with Strapi.

Installation

npm i @grace-studio/next-strapi

or

yarn add @grace-studio/next-strapi

Usage example

import {
  NextStrapiConfig,
  NextStrapi,
  CollectionItem,
} from '@grace-studio/next-strapi';

const config: NextStrapiConfig = {
  apiToken: process.env.STRAPI_API_TOKEN!,
  apiUrl: process.env.NEXT_PUBLIC_STRAPI_API_URL!,
};

// Create instance of NextStrapi
const api = NextStrapi.create(config);

// Get single item with id = global
const {
  data: [global],
  meta,
} = await api.get.item({
  apiId: 'global',
  locale: 'en',
  populateQueryObject: {
    populate: '*',
  },
});

// Get collection with id = pages
const { data: pages } = await api.get.collection({
  apiId: 'pages',
  locale: 'en',
  populateQueryObject: {
    populate: '*',
  },
});

// Get collections paths with id = pages
const { data: pages } = await api.fetch.collectionPaths({ apiId: 'pages' });

const paths = pages.map((page: CollectionItem) => {
  const { slug } = page;

  return {
    params: { slug },
  };
});

// The expected output type from the api can be provided to the methods
type GlobalItem = {
  id: number;
  title: string;
  slug: string;
};

type Meta = {
  pagination: {
    page: number;
    pageSize: number;
    pageCount: number;
    total: number;
  };
};

const {
  data: [global],
  meta,
} = await api.get.item<GlobalItem, Meta>({
  apiId: 'global',
  locale: 'en',
  populateQueryObject: {
    populate: '*',
  },
});

// Get menu with slug = main-menu
const { data: menu } = await api.get.menus({
  slug: 'main-menu',
});
2.0.0

4 months ago

1.2.0

5 months ago

1.1.0

8 months ago

1.0.1

8 months ago

0.3.0

11 months ago

0.1.0

2 years ago

0.0.1

2 years ago