Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: medusajs/nextjs-starter-medusa
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2cef488a12e368e4f0759df183b05b5f2fb05bb8
Choose a base ref
..
head repository: medusajs/nextjs-starter-medusa
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ef41ddca38bc3753fd422ad844b5c67186ca55b9
Choose a head ref
Showing with 42 additions and 2 deletions.
  1. +1 −0 src/lib/config.ts
  2. +41 −2 src/lib/sdk/index.ts
1 change: 1 addition & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -15,4 +15,5 @@ export const medusaClient = new OldMedusa({

export const newClient = new NewMedusa({
baseUrl: MEDUSA_BACKEND_URL,
debug: process.env.NODE_ENV === "development",
})
43 changes: 41 additions & 2 deletions src/lib/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import qs from "qs"

// TODO: Add debug logging
export type Logger = {
error: (...messages: string[]) => void
warn: (...messages: string[]) => void
info: (...messages: string[]) => void
debug: (...messages: string[]) => void
}

export type Config = {
baseUrl: string
globalHeaders?: Record<string, string>
@@ -10,6 +16,8 @@ export type Config = {
storageKey?: string
storageMethod?: "local" | "session" | "memory"
}
logger?: Logger
debug?: boolean
}

type FetchParams = Parameters<typeof fetch>
@@ -29,14 +37,31 @@ const toBase64 = (str: string) => {
return Buffer.from(str).toString("base64")
}

const sanitizeHeaders = (headers: any) => {
return { ...headers, Authorization: "<REDACTED>" }
}

// TODO: Add support for retries and timeouts
class Client {
public fetch: ClientFetch
private logger: Logger

private DEFAULT_JWT_STORAGE_KEY = "medusa_auth_token"
private token = ""

constructor(config: Config) {
const logger = config.logger || {
error: console.error,
warn: console.warn,
info: console.info,
debug: console.debug,
}

this.logger = {
...logger,
debug: config.debug ? logger.debug : () => {},
}

this.fetch = this.initClient(config)
}

@@ -49,6 +74,11 @@ class Client {
...config.globalHeaders,
}

this.logger.debug(
"Initiating Medusa client with default headers:\n",
`${JSON.stringify(sanitizeHeaders(defaultHeaders), null, 2)}\n`
)

return (
input: FetchParams[0],
init?: FetchParams[1] & { query?: Record<string, any> }
@@ -70,8 +100,17 @@ class Client {
}
}

this.logger.debug(
"Performing request to:\n",
`URL: ${normalizedInput.toString()}\n`,
`Headers: ${JSON.stringify(sanitizeHeaders(headers), null, 2)}\n`
)

// TODO: Make response a bit more user friendly (throw errors, return JSON if resp content type is json, etc.)
return fetch(normalizedInput, { ...init, headers })
return fetch(normalizedInput, { ...init, headers }).then((resp) => {
this.logger.debug(`Received response with status ${resp.status}\n`)
return resp
})
}
}