Giter VIP home page Giter VIP logo

Comments (14)

TheRealFlyingCoder avatar TheRealFlyingCoder commented on May 16, 2024 2

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.

gwax avatar gwax commented on May 16, 2024 2

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.

FelixTellmann avatar FelixTellmann commented on May 16, 2024 1

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

from shopify-api-js.

Wangggym avatar Wangggym commented on May 16, 2024

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.

Wangggym avatar Wangggym commented on May 16, 2024

It's very important for ts developer.

from shopify-api-js.

FelixTellmann avatar FelixTellmann commented on May 16, 2024

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 ?

from shopify-api-js.

TheRealFlyingCoder avatar TheRealFlyingCoder commented on May 16, 2024

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.

FelixTellmann avatar FelixTellmann commented on May 16, 2024

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.

TheRealFlyingCoder avatar TheRealFlyingCoder commented on May 16, 2024

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.

FelixTellmann avatar FelixTellmann commented on May 16, 2024

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.

github-actions avatar github-actions commented on May 16, 2024

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.

noahsark769 avatar noahsark769 commented on May 16, 2024

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

from shopify-api-js.

github-actions avatar github-actions commented on May 16, 2024

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.

github-actions avatar github-actions commented on May 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.