Coder Social home page Coder Social logo

shopify_deno's Introduction

shopify_deno

Shopify Integration for Deno.

How to use

This library has an abstraction of the application and the use of the API.

Creating the application instances

This implementation allows several applications in the same server instance in a simple way. Routes are automatically generated.

Example:

import { req, res, Server } from "https://deno.land/x/faster/mod.ts";
import {
  ShopifyAPI,
  ShopifyApp,
  WebHookCall,
} from "https://deno.land/x/shopify_deno/mod.ts";
const server = new Server();
server.get(
  "/my_app_1_index", //shopifyApp1 home route
  async (ctx: any, next: any) => {
    ctx.res.body = "My Shopify Deno";
    await next();
  },
);

const shopifyApp1 = new ShopifyApp(
  {
    api_key: "79e4871756c98ccf48ac647c724022e1",
    api_secret: "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",
    scopes: "read_products,read_orders",
    host: "https://xxx.ngrok.io", //Without ending with "/"
    namespace: "my_app_1_ns", //you can instantiate different apps on the same server with different namespaces
    home_route: "/my_path1", //Simple path. You cannot have query parameters like my_path1?a=1&b=2.
    //This path will receive the "shop" and "session" parameters, in the format: my_path1?shop=example.myshopify.com&session=e8aa2f7522a9ccaa1
    webhooks: [ //OPTIONAL
      {
        topic: "orders/create", //Only one webhook per topic is allowed
        func: (hook: WebHookCall) => {
          //avoid using await here, prevent shopify webhook timeout is important
          console.log(hook.data);
          console.log(hook.shop);
        },
      },
    ],
  },
  server,
);
await server.listen({ port: 80 });

See type "ShopifyAppOptions" for full options.

Configure auto renerated routes

In Shopify Configs, configure:

  1. App URL:
"host"/"namespace"/install
  1. Allowed redirection URL(s):
"host"/"namespace"/auth
"host"/"home_route"
  1. Privacy policy:
"host"/"namespace"/privacy_policy
  1. Customer data request endpoint:
"host"/"namespace"/webhooks/client_data
  1. Customer data erasure endpoint:
"host"/"namespace"/webhooks/delete_client
  1. Shop data erasure endpoint:
"host"/"namespace"/webhooks/delete_shop

Each webhook is automatically placed in (for information only, the library registers automatically with the api):

"host"/"namespace"/webhooks/"topic"

Using the API

This library abstracts the communication and handles the "API rate limit".

Examples:

const access_token = await shopifyApp1.getAccessToken(shop);
const api = new ShopifyAPI(shop, access_token); // 'shop' it's something like myexampleshop.myshopify.com, 'shop' and 'access_token' comes from 'userTokenFunc'
const data1 = await api.graphQL(`
{
    products(first: 10, query:"tag:*(MY_TAG1)*"){
      edges{
        node {
          tags
        }
      }
    }
}
`);
const data2 = await api.get(
  `/admin/api/2021-01/script_tags.json`,
);
const data3 = await this.post(`/admin/api/2021-01/script_tags.json`, {
  "script_tag": {
    "event": "onload",
    "src": "https://xxx.ngrok.io/myscript.js",
    "cache": true,
  },
});

All imports

import {
  ShopifyAPI,
  ShopifyApp,
  ShopifyAppOptions,
  UserTokenFunc,
  WebHook,
  WebHookCall,
  WebHookFunc,
} from "https://deno.land/x/shopify_deno/mod.ts";
import { req, res, Server } from "https://deno.land/x/faster/mod.ts";

This library has some more abstracted methods for some applications I created, explore the ShopifyAPI class (may be useful for you). Some examples are: "includeScripts" and "searchTags".

About

Author: Henrique Emanoel Viana, a Brazilian computer scientist, enthusiast of web technologies, cel: +55 (41) 99999-4664. URL: https://sites.google.com/view/henriqueviana

Improvements and suggestions are welcome!

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.