Skip to content

Commit

Permalink
Fix nosto-js-sdk usage
Browse files Browse the repository at this point in the history
  • Loading branch information
timowestnosto committed Nov 5, 2024
1 parent 7c204ce commit d2eb210
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 60 deletions.
18 changes: 11 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"src"
],
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"@nosto/nosto-js": "*"
},
"devDependencies": {
Expand All @@ -47,8 +49,6 @@
"eslint-plugin-promise": "^7.1.0",
"eslint-plugin-react": "^7.33.2",
"prettier": "^3.3.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router": "^6.25.1",
"react-router-dom": "^6.25.1",
"rimraf": "^6.0.1",
Expand All @@ -65,8 +65,7 @@
".": {
"import": "./dist/index.es.js",
"require": "./dist/index.umd.js"
},
"./shopify-hydrogen": "./dist/index.es.js"
}
},
"bugs": {
"url": "https://github.com/Nosto/nosto-react/issues"
Expand Down
8 changes: 4 additions & 4 deletions spec/mocks/mock-api.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { vi } from "vitest"
import { jsonMockData } from "./mock-data"
import { Product } from "../../src"
import { API, Session, Action, PageType, Data } from "@nosto/nosto-js/client"
import { API, Session, Action, PageType, TaggingData } from "@nosto/nosto-js/client"

function normalizeProduct(data: Product | string) {
return typeof data === "string" ? { product_id: data } : data
}

let latestActionData: Partial<Data>
let latestActionData: Partial<TaggingData>

function newSession(placements: string[]) {
const data: Partial<Data> & { responseMode?: string} = {}
const data: Partial<TaggingData> & { responseMode?: string} = {}

function newAction(pageType: PageType, overrides?: Partial<Data>) {
function newAction(pageType: PageType, overrides?: Partial<TaggingData>) {
const actionData = { ...data, ...overrides, pageType }
latestActionData = actionData

Expand Down
9 changes: 5 additions & 4 deletions spec/useLoadClientScript.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { renderHook } from "@testing-library/react"
import { useLoadClientScript } from "../src/hooks/useLoadClientScript"
import scriptLoader from "../src/hooks/scriptLoader"
import "@testing-library/jest-dom/vitest"
import { getNostoWindow, isNostoLoaded, reloadNosto } from "@nosto/nosto-js"
import { nostojs, getNostoWindow, isNostoLoaded } from "@nosto/nosto-js"
import { reloadNosto } from "@nosto/nosto-js/testing"

function loadClientScript(merchant: string) {
const script = document.createElement("script")
Expand All @@ -30,7 +31,7 @@ describe("useLoadClientScript", () => {

it("loads client script", async () => {
const hook = renderHook(() => useLoadClientScript({ account: testAccount }))
await new Promise(window.nostojs)
await new Promise(nostojs)

hook.rerender()
expect(hook.result.current.clientScriptLoaded).toBe(true)
Expand All @@ -41,7 +42,7 @@ describe("useLoadClientScript", () => {
it("support custom script loaders", async () => {
const customScriptLoader = vi.fn(scriptLoader)
const hook = renderHook(() => useLoadClientScript({ account: testAccount, scriptLoader: customScriptLoader }))
await new Promise(window.nostojs)
await new Promise(nostojs)

hook.rerender()
expect(customScriptLoader).toHaveBeenLastCalledWith(`https://connect.nosto.com/include/${testAccount}`, {
Expand All @@ -63,7 +64,7 @@ describe("useLoadClientScript", () => {

it("set loaded state to true when client is loaded externally before", async () => {
await loadClientScript(testAccount)
expect(window.nosto).toBeDefined()
expect(isNostoLoaded()).toBeTruthy()

const { result } = renderHook(() => useLoadClientScript({ loadScript: false, account: testAccount }))
expect(result.current.clientScriptLoaded).toBe(true)
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useLoadClientScript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState, useEffect } from "react"
import type { NostoProviderProps } from "../components/NostoProvider"
import scriptLoaderFn from "./scriptLoader"
import { init, initNostoStub, isNostoLoaded, nostojs, reloadNosto } from "@nosto/nosto-js"
import { init, initNostoStub, isNostoLoaded, nostojs } from "@nosto/nosto-js"
import { reloadNosto } from "@nosto/nosto-js/testing"

type NostoScriptProps = Pick<NostoProviderProps, "account" | "host" | "shopifyMarkets" | "loadScript" | "scriptLoader">

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useNostoOrder.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { snakeize } from "../utils/snakeize"
import { Order } from "../types"
import { useRenderCampaigns } from "./useRenderCampaigns"
import { useNostoApi } from "./useNostoApi"
import { ToCamelCase } from "../utils/types"
import { WebsiteOrder as Order } from "@nosto/nosto-js/client"

/**
* @group Hooks
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useNostoProduct.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Product } from "../types"
import { useNostoApi } from "./useNostoApi"
import { useRenderCampaigns } from "./useRenderCampaigns"
import { Product } from "@nosto/nosto-js/client"

/**
* @group Hooks
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useNostoSession.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { snakeize } from "../utils/snakeize"
import { Cart as CartSnakeCase, Customer as CustomerSnakeCase } from ".."
import { PushedCustomer as CustomerSnakeCase, Cart as CartSnakeCase } from "@nosto/nosto-js/client"
import { ToCamelCase } from "../utils/types"
import { useNostoContext } from "./useNostoContext"
import { useDeepCompareEffect } from "./useDeepCompareEffect"
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useRenderCampaigns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Recommendation } from "../types"
import { useNostoContext } from "./useNostoContext"
import { RecommendationComponent } from "../context"
import { ActionResponse, API } from "@nosto/nosto-js/client"
import { nostojs } from "@nosto/nosto-js"
import { nostojs, isNostoLoaded } from "@nosto/nosto-js"

type CampaignData = Pick<ActionResponse, "campaigns" | "recommendations">

Expand All @@ -20,6 +20,7 @@ function RecommendationComponentWrapper(props: {
}

function injectCampaigns(data: CampaignData) {
// @ts-expect-error not defined
if (!window.nostojs) {
throw new Error("Nosto has not yet been initialized")
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { Product, PushedCustomer as Customer } from "@nosto/nosto-js/client"
export type { Cart, Order, Recommendation } from "./types"
export type { Product, PushedCustomer as Customer, Cart, WebsiteOrder as Order } from "@nosto/nosto-js/client"
export type { Recommendation } from "./types"
export { type ScriptLoadOptions } from "./hooks/scriptLoader"
export { NostoContext, type NostoContextType } from "./context"
export { useNostoContext } from "./hooks/useNostoContext"
Expand Down
35 changes: 1 addition & 34 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import type { API, CartItem, ConversionItem, OrderCustomer, PushedProduct } from "@nosto/nosto-js/client"

declare global {
interface Window {
nosto?: {
reload(settings: unknown): void
}
nostojs: {
(callback: (api: API) => void): void
q?: unknown[]
}
}
}
import type { PushedProduct } from "@nosto/nosto-js/client"

/**
* @group Types
Expand All @@ -24,24 +12,3 @@ export interface Recommendation {
source_product_ids: string[]
params: unknown
}

/**
* @group Types
*/
export interface Order {
created_at?: Date
external_order_ref: string
info?: OrderCustomer
items: ConversionItem[]
order_status?: string
order_status_label?: string
payment_provider: string
}

/**
* @group Types
*/
export interface Cart {
hcid?: string
items: CartItem[]
}

0 comments on commit d2eb210

Please sign in to comment.