shopify-api-js
shopify-api-js copied to clipboard
Typescript types of REST Shopify Admin Objects
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)
I'd like to have types of Shopify REST objects so that I can have reliable types and make more reliable Shopify apps.
It's very important for ts developer.
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:
- 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",
},
});
- 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 thepath
from string to the actualpath
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 ?
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.
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' })
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",
},
});
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?
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.
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.
Bumping this! Would be great to have these types exposed externally for library consumers
You can install ‘shopify-typed-node-api’ as an alternative
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.
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!
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;