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

types-4-strapi

v1.0.22

Published

Typescript interface generator for Strapi 4 models

Downloads

337

Readme

Types-4-Strapi

Typescript interface generator for Strapi 4 models.

Install locally

npm i --save-dev types-4-strapi

Add t4s to your scripts:

"scripts": {
  "develop": "strapi develop",
  "start": "strapi start",
  "build": "strapi build",
  "strapi": "strapi",
  "t4s": "t4s"
}

Then run with:

npm run t4s

Install globally

npm i -g types-4-strapi

Then run with:

t4s

Attributes

For some inscrutable reason, Strapi 4 returns objects where all the properties (aside from id) are wrapped into an attributes object. The resulting interfaces will look like this:

{
  id: number;
  attributes: {
    username: string;
    email: string;
    provider: string;
    confirmed: boolean;
    blocked: boolean;
    createdAt: Date;
    updatedAt: Date;
  }
}

However, for some even more inscrutable reason, sometimes the same object is returned "flattened", without an attributes object. This is the case, for instance, for the /api/users endpoint, which returns an array of Users with the following structure:

{
  id: number;
  username: string;
  email: string;
  provider: string;
  confirmed: boolean;
  blocked: boolean;
  createdAt: Date;
  updatedAt: Date;
}

The same "flat" structure is also required when submitting the body of POST and PUT requests. Here is an example using fetch.

// correct
await fetch('https://project.com/api/users', {
  method: 'POST',
  body: JSON.stringify({
    username: 'Jon Snow',
    email: '[email protected]',
  }),
});

// incorrect
await fetch('https://project.com/api/users', {
  method: 'POST',
  body: JSON.stringify({
    attributes: {
      username: 'Jon Snow',
      email: '[email protected]',
    },
  }),
});

In these cases, rather than creating completely new types, we recommend that you simply 'extract' the type of the attribute object from the entity's interface using indexed access types.

type UserAttributes = User['attributes'];

await fetch('https://project.com/api/users', {
  method: 'POST',
  body: {
    username: 'Jon Snow',
    email: '[email protected]',
  } as UserAttributes
});

If you are using strapi-plugin-transformer to remove the attributes key from all responses, use the following generic transformation type to be able to utilise the interfaces generated by types-4-strapi:

type Transformed<A extends { attributes: any }> = A['attributes'] & {
  id: number;
};

Usage:

const response = await fetch('https://project.com/api/users');

const json = await response.json();

const users = json.data as Transformed<User>[];