@bss-sbc/shopify-api-fetcher
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

Shopify API Fetcher

From: BSS Group > BSS Commerce > Division SBC

Node version

NPM Download Stats

API References

  • Rest.safeFetch: (domain: string, url: RequestInfo, params?: RequestInit) => Promise<Response>: use this instead of normal fetch, with additional parameter domain, which specify a store.

  • GraphQL.safeFetch: (domain: string, token: string, { query: string, variables?: Object }) => Promise<Unknown>: use this instead of normal fetch, with additional parameter domain, which specify a store.

Error Handling

GraphqlError

  • If Graphql response status is not 2xx, throw GraphqlError
new GraphqlError('', {
    cause: {
        status: shopifyResult.status,
        errors: JSON_RESULT.errors,
        userErrors: false,
        cost: JSON_RESULT.extensions ? JSON_RESULT.extensions.cost : null,
    },
})
  • If throttled but not configured to retry, throw GraphqlError
new GraphqlError('', {
    cause: {
        status: isThrottled ? 'throttled' : shopifyResult.status,
        errors: JSON_RESULT.errors,
        userErrors: false,
        cost: JSON_RESULT.extensions ? JSON_RESULT.extensions.cost : null,
    },
})
  • If there are user errors, throw GraphqlError
new GraphqlError('', {
    cause: {
        status: shopifyResult.status,
        errors: false,
        userErrors: JSON_RESULT.data[JSON_RESULT_FIRST_KEY].userErrors,
        cost: JSON_RESULT.extensions ? JSON_RESULT.extensions.cost : null,
    },
})
  • If uncaught error, throw GraphqlError
new GraphqlError('', { cause: shopifyError })

Example

const { GraphQL, Rest } = require("@bss-sbc/shopify-api-fetcher");

const DOMAIN="...replace this....myshopify.com"
const TOKEN="...replace this..."
const API_VERSION="2022-10"

async function main() {
    await GraphQL.safeFetch(DOMAIN, TOKEN, {
        query: `
        query queryShop {
            shop {
                name
                email
                contactEmail
                myshopifyDomain
                ianaTimezone
                currencyCode
                currencyFormats {
                    moneyFormat
                }
            }
        }
        `,
    }).then(data => console.log(JSON.stringify(data)));

    await Rest.safeFetch(DOMAIN, `https://${DOMAIN}/admin/api/${API_VERSION}/shop.json`, {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            'X-Shopify-Access-Token': TOKEN,
        }
    }).then(response => response.json()).then(data => console.log(JSON.stringify(data)));
}

main().finally(() => {
    process.exit(0);
});
  • [x] REST
  • [ ] Time-to-live REST
  • [x] GraphQL
  • [x] Time-to-live GraphQL

Readme

Keywords

none

Package Sidebar

Install

npm i @bss-sbc/shopify-api-fetcher

Weekly Downloads

7

Version

2.0.4

License

ISC

Unpacked Size

27.3 kB

Total Files

14

Last publish

Collaborators

  • hiepnguyennk
  • xhoang0509
  • midadean