Skip to content

Instantly share code, notes, and snippets.

@TimNZ
Last active April 8, 2020 22:22
Show Gist options
  • Select an option

  • Save TimNZ/93c94888eef7e2bee67860238929d60d to your computer and use it in GitHub Desktop.

Select an option

Save TimNZ/93c94888eef7e2bee67860238929d60d to your computer and use it in GitHub Desktop.

Revisions

  1. TimNZ revised this gist Apr 8, 2020. 1 changed file with 34 additions and 27 deletions.
    61 changes: 34 additions & 27 deletions cocart-node.js
    Original file line number Diff line number Diff line change
    @@ -1,44 +1,51 @@
    const axios = require('axios').default
    const axiosCookieJarSupport = require('axios-cookiejar-support').default;
    const CookieJar = require('tough-cookie').CookieJar;
    const axios = require("axios").default
    const axiosCookieJarSupport = require("axios-cookiejar-support").default
    const CookieJar = require("tough-cookie").CookieJar
    const assert = require('assert')
    let instanceId = 1
    const createAxiosInstance = () => {
    const axiosInstance = axios.create({
    baseURL: 'https://test.site/wp-json/cocart/v1',
    baseURL: "https://test.site/wp-json/cocart/v1",
    withCredentials: true,
    });
    })
    axiosCookieJarSupport(axiosInstance)
    axiosInstance.defaults.jar = new CookieJar()
    axiosInstance.instanceId = instanceId++
    return axiosInstance


    }

    const getCart = async (axiosInstance) => axiosInstance.get('/get-cart')
    .then(response => {
    console.log(axiosInstance.instanceId,'get-cart',response.data)
    const getCart = async (axiosInstance) =>
    axiosInstance.get("/get-cart").then((response) => {
    console.log(axiosInstance.instanceId, "get-cart", response.data)
    return response.data
    })
    .catch(err => console.log(err))

    const addItem = async (axiosInstance,productId) => axiosInstance.post('/add-item',{
    product_id: productId
    })
    .then(response => { console.log(axiosInstance.instanceId,'add-item',response.data); return response.headers})

    const clearCart = async (axiosInstance) => axiosInstance.post('/clear')
    .then(response => console.log(axiosInstance.instanceId,'clear',response.data));

    const addItem = async (axiosInstance, productId) =>
    axiosInstance
    .post("/add-item", {
    product_id: productId,
    })
    .then((response) => {
    console.log(axiosInstance.instanceId, "add-item", response.data)
    return response.data
    })

    const clearCart = async (axiosInstance) =>
    axiosInstance.post("/clear").then((response) => {
    console.log(axiosInstance.instanceId, "clear", response.data)
    return response.data
    })

    async function run(axiosInstance)
    {
    await getCart(axiosInstance)
    await addItem(axiosInstance,1)
    await getCart(axiosInstance)
    await addItem(axiosInstance,2)
    await getCart(axiosInstance)
    async function run(axiosInstance) {
    let cart = await getCart(axiosInstance)
    assert(cart.length == 0)
    await addItem(axiosInstance, 1)
    cart = await getCart(axiosInstance)
    assert(cart.totals.subtotal == '$10.00')
    await addItem(axiosInstance, 2)
    cart = await getCart(axiosInstance)
    assert(cart.totals.subtotal == '$20.00')
    }

    run(createAxiosInstance())
    run(createAxiosInstance())

  2. TimNZ created this gist Apr 8, 2020.
    44 changes: 44 additions & 0 deletions cocart-node.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    const axios = require('axios').default
    const axiosCookieJarSupport = require('axios-cookiejar-support').default;
    const CookieJar = require('tough-cookie').CookieJar;
    let instanceId = 1
    const createAxiosInstance = () => {
    const axiosInstance = axios.create({
    baseURL: 'https://test.site/wp-json/cocart/v1',
    withCredentials: true,
    });
    axiosCookieJarSupport(axiosInstance)
    axiosInstance.defaults.jar = new CookieJar()
    axiosInstance.instanceId = instanceId++
    return axiosInstance


    }

    const getCart = async (axiosInstance) => axiosInstance.get('/get-cart')
    .then(response => {
    console.log(axiosInstance.instanceId,'get-cart',response.data)
    })
    .catch(err => console.log(err))

    const addItem = async (axiosInstance,productId) => axiosInstance.post('/add-item',{
    product_id: productId
    })
    .then(response => { console.log(axiosInstance.instanceId,'add-item',response.data); return response.headers})

    const clearCart = async (axiosInstance) => axiosInstance.post('/clear')
    .then(response => console.log(axiosInstance.instanceId,'clear',response.data));


    async function run(axiosInstance)
    {
    await getCart(axiosInstance)
    await addItem(axiosInstance,1)
    await getCart(axiosInstance)
    await addItem(axiosInstance,2)
    await getCart(axiosInstance)
    }

    run(createAxiosInstance())
    run(createAxiosInstance())