Comments (14)
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.
from shopify-api-js.
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;
from shopify-api-js.
You can install ‘shopify-typed-node-api’ as an alternative
from shopify-api-js.
I'd like to have types of Shopify REST objects so that I can have reliable types and make more reliable Shopify apps.
from shopify-api-js.
It's very important for ts developer.
from shopify-api-js.
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 theparams
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 ?
from shopify-api-js.
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' })
from shopify-api-js.
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",
},
});
from shopify-api-js.
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?
from shopify-api-js.
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.
from shopify-api-js.
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.
from shopify-api-js.
Bumping this! Would be great to have these types exposed externally for library consumers
from shopify-api-js.
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.
from shopify-api-js.
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!
from shopify-api-js.
Related Issues (20)
- Support pagination when searching customers with admin rest api HOT 1
- Unable to create customer with password using customerCreate on admin API node client HOT 1
- Expose RemixRestClient and GraphQLClient types for @shopify/shopify-app-remix HOT 1
- Managed Installation is broken when reinstalling from Admin's Settings. HOT 8
- GraphQL Codegen Fails with Multiple Errors When Generating Types for Shopify API HOT 4
- Upgrade to new Shopify API version without upgrading `@shopify/shopify-api` package HOT 1
- Documentation pages return 404 HOT 1
- [API] Invalid API key or access token (unrecognized login or wrong password) Error HOT 3
- A new release of the Shopify API is due soon HOT 1
- Keep getting INTERNAL_SERVER_ERROR on the response of `productVariantsBulkCreate` query HOT 1
- NEW BUG: storeAvailability locations missing HOT 2
- Customer deletion error: Disconnected before secure TLS was established HOT 1
- shopify.session.getCurrentId() fails to read request headers from Next.js app router HOT 6
- Multiple Webhook Handlers Error
- Rest resource `Shop.all()` returns the complete response HOT 3
- A new release of the Shopify API occurred
- April 2024 API version HOT 4
- Recurring Application Charge currency being returned as weird object with each letter as a different value HOT 1
- ShopifyApp::RequireKnownShop not worked for Shopify API version 2024-04 HOT 1
- `shop/update` webhook not triggered when editing `marketing_sms_consent_enabled_at_checkout` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shopify-api-js.