shopify-api-js icon indicating copy to clipboard operation
shopify-api-js copied to clipboard

Typescript types of REST Shopify Admin Objects

Open eugene-kim opened this issue 3 years ago • 8 comments

Overview

It would be great to have types of each of the REST Admin API Shopify objects (e.g. Orders, Products, etc...) defined in this library (or maybe in a separate dedicated library of types) so that partners can have a type safe way of handling Shopify REST API responses.

This library doesn't have these types and other community based libraries that have defined types are unreliable and oftentimes out of date.

Type

  • [x] New feature
  • [] Changes to existing features

Motivation

What inspired this feature request? What problems were you facing?

I'd like to have types of Shopify REST objects so that I can have reliable types and make more reliable Shopify apps.


Checklist

  • [x] I have described this feature request in a way that is actionable (if possible)

eugene-kim avatar Jun 06 '21 23:06 eugene-kim

I'd like to have types of Shopify REST objects so that I can have reliable types and make more reliable Shopify apps.

Wangggym avatar Oct 21 '21 09:10 Wangggym

It's very important for ts developer.

Wangggym avatar Oct 21 '21 09:10 Wangggym

I have fully extracted the types from the docs page, by parsing each api path & method individually. I've tried upgrading the package to include the types as a non-breaking change or adding the types manually per request. However, due to TS limitations (max 25 union type conditionals) I'm not finding a good solution to implement the types in a user-friendly way.

My current ideas to solve the problem are these two:

  1. Adding the Types per request (we can still have type hints for the path property.)
const data = await ShopifyRestClient.get<GetProduct>({
  path: `product`,
  query: {
    handle: "my-favorite-product",
  },
});
  1. The second approach is to add a full ORM into the RestClient similar how Prisma solves the problem. Using this approach would be fully Typed by default, making it much more user friendly, while its still possible to be fully backwards compatible. The only change on the params would be to change the path from string to the actual path input parameters (i.e. product_id, variant_id, etc.)
const data = await ShopifyRestClient.Product.findMany({
  query: {
    handle: "my-favorite-product",
  },
});

const data = await ShopifyRestClient.Product.findOne({
  path: {
    product_id: 123456768
  }
  query: {
    handle: "my-favorite-product",
  },
});

Anybody got any other ideas on how to add the Types to the current Rest params ?

FelixTellmann avatar Dec 15 '21 06:12 FelixTellmann

I'm honestly shocked this hasn't been implemented yet?

https://github.com/Shopify/shopify-node-api/tree/main/src/rest-resources/2022-01

The rest resources folder implements classes for every object we are looking for, and could easily be extracted out?

Adding generics to the Rest / Graphql functions would be a matter of a couple hours work... and save every admin developer a ridiculous amount of headaches.

TheRealFlyingCoder avatar Mar 29 '22 06:03 TheRealFlyingCoder

The third and simpler option on Felix's last message is to simply define each path option (Because you know them all) as a wrapper function on .get() and define the return type

restClient.products.get(): Promise<RequestReturn<ProductsBody>> = restClient.get({ path: '/products' })

TheRealFlyingCoder avatar Mar 29 '22 06:03 TheRealFlyingCoder

I've created a fork with a typed version of the package you can install with npm install shopify-typed-node-api or yarn add shopify-typed-node-api. I have extracted all types directly from the Documentation pages. Unfortunately, they are some errors here and there on their docs site that have translated into the package. And I haven't gotten around to create any documentations for the update.

Please feel free to try it out. The API works exactly as it is in this package. and you can add typings on a per request basis as below:

import { Product } from "shopify-typed-node-api/dist/clients/rest/dataTypes";

const ShopifyRest = new Shopify.Clients.Rest(shop, accessToken);

ShopifyRest.get<Product.Get>({
      path: "products",
      query: {
        limit: "250",
      },
});

FelixTellmann avatar Mar 29 '22 07:03 FelixTellmann

Hey Felix, That's awesome work!

If there are errors you didn't "really" need to fork the whole package, it would have been possible to simply export an extended RequestReturn interface and the other types to use your package like:

interface GetProducts extends RequestReturn {
  body:  { products: Product[] }
}
import { GetProducts } from "shopify-typed-node-api/dist/clients/rest/dataTypes";
import Shopify from "@shopify/shopify-api";

const ShopifyRest = new Shopify.Clients.Rest(shop, accessToken);

const response: GetProducts = await ShopifyRest.get({
      path: "products",
      query: {
        limit: "250",
      },
});

Maybe a PR here would be possible to try and just bring those types in?

TheRealFlyingCoder avatar Mar 29 '22 20:03 TheRealFlyingCoder

I've had a look through the rest types as provided here: https://github.com/Shopify/shopify-node-api/tree/main/src/rest-resources/2022-01

They seem to be pretty incomplete as well. I had planned a PR a while ago when I first did the update, but my initial implementation required a complete rewrite of the API.

I will spend some time this weekend and do a PR.

FelixTellmann avatar Mar 30 '22 16:03 FelixTellmann

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Oct 06 '22 02:10 github-actions[bot]

Bumping this! Would be great to have these types exposed externally for library consumers

noahsark769 avatar Oct 14 '22 19:10 noahsark769

You can install ‘shopify-typed-node-api’ as an alternative

FelixTellmann avatar Oct 14 '22 19:10 FelixTellmann

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Jan 31 '23 02:01 github-actions[bot]

We are closing this issue because it has been inactive for a few months. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on since it was submitted, then it seems other issues have taken priority.

If you still encounter this issue with the latest stable version, please reopen using the issue template. You can also contribute directly by submitting a pull request– see the CONTRIBUTING.md file for guidelines

Thank you!

github-actions[bot] avatar Feb 14 '23 02:02 github-actions[bot]

I know that I'm over a year late but for folks coming across this later, an alternative is to use instance types from the shopify rest client object.

If you have a function that returns a shopify client:

function newShopifyApi() {
  return shopifyApi( {
    ...
    apiVersion: LATEST_API_VERSION,
    ...
  });
}

Then you can extract the types of resources from that functions return type:

type RestResources = ReturnType<typeof newShopifyApi>["rest"];
export type RestResource<RestResourceType extends keyof RestResources> =
  RestResourceType extends keyof RestResources
    ? InstanceType<RestResources[RestResourceType]>
    : never;

gwax avatar Jan 01 '24 08:01 gwax