Skip to content

Commit

Permalink
feat: Create the Medusa API SDK as js-sdk package
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski committed May 15, 2024
1 parent 13ce60b commit 507a54a
Show file tree
Hide file tree
Showing 13 changed files with 963 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-cherries-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/js-sdk": patch
---

Introduce a js-sdk package for the Medusa API
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports = {
"./packages/core/orchestration/tsconfig.json",
"./packages/core/workflows-sdk/tsconfig.spec.json",
"./packages/core/modules-sdk/tsconfig.spec.json",
"./packages/core/js-sdk/tsconfig.spec.json",
"./packages/core/types/tsconfig.spec.json",
"./packages/core/utils/tsconfig.spec.json",
"./packages/core/medusa-test-utils/tsconfig.spec.json",
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions packages/core/js-sdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
transform: {
"^.+\\.[jt]s?$": "@swc/jest",
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`, `json`],
}
40 changes: 40 additions & 0 deletions packages/core/js-sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@medusajs/js-sdk",
"version": "0.0.1",
"description": "SDK for the Medusa API",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/core/js-sdk"
},
"engines": {
"node": ">=18"
},
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"cross-env": "^5.2.1",
"jest": "^29.6.3",
"msw": "^2.3.0",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
"dependencies": {
"@medusajs/types": "^1.11.16",
"qs": "^6.12.1"
},
"scripts": {
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"build": "rimraf dist && tsc --build",
"test": "jest --passWithNoTests --runInBand --bail --forceExit",
"watch": "tsc --build --watch"
}
}
145 changes: 145 additions & 0 deletions packages/core/js-sdk/src/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { http, HttpResponse } from "msw"
import { setupServer } from "msw/node"

import { Client, FetchError } from "../client"

const baseUrl = "https://someurl.com"

// This is just a network-layer mocking, it doesn't start an actual server
const server = setupServer(
http.get(`${baseUrl}/test`, ({ request, params, cookies }) => {
return HttpResponse.json({
test: "test",
})
}),
http.get(`${baseUrl}/throw`, ({ request, params, cookies }) => {
return new HttpResponse(null, {
status: 500,
statusText: "Internal Server Error",
})
}),
http.get(`${baseUrl}/header`, ({ request, params, cookies }) => {
if (
request.headers.get("X-custom-header") === "test" &&
request.headers.get("Content-Type") === "application/json"
) {
return HttpResponse.json({
test: "test",
})
}
}),
http.get(`${baseUrl}/apikey`, ({ request, params, cookies }) => {
console.log(request.headers.get("authorization"))
if (request.headers.get("authorization")?.startsWith("Basic")) {
return HttpResponse.json({
test: "test",
})
}
}),
http.get(`${baseUrl}/pubkey`, ({ request, params, cookies }) => {
if (request.headers.get("x-medusa-pub-key") === "test-pub-key") {
return HttpResponse.json({
test: "test",
})
}
}),
http.post(`${baseUrl}/create`, async ({ request, params, cookies }) => {
return HttpResponse.json(await request.json())
}),
http.delete(`${baseUrl}/delete/123`, async ({ request, params, cookies }) => {
return HttpResponse.json({ test: "test" })
}),
http.all("*", ({ request, params, cookies }) => {
return new HttpResponse(null, {
status: 404,
statusText: "Not Found",
})
})
)

describe("Client", () => {
let client: Client
beforeAll(() => {
client = new Client({
baseUrl,
})

server.listen()
})
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

describe("header configuration", () => {
it("should allow passing custom request headers while the defaults are preserved", async () => {
const resp = await client.fetch<any>("header", {
headers: { "X-custom-header": "test" },
})

expect(resp).toEqual({ test: "test" })
})

it("should allow passing global headers", async () => {
const headClient = new Client({
baseUrl,
globalHeaders: {
"X-custom-header": "test",
},
})

const resp = await headClient.fetch<any>("header")
expect(resp).toEqual({ test: "test" })
})

it("should allow setting an API key", async () => {
const authClient = new Client({
baseUrl,
apiKey: "test-api-key",
})

const resp = await authClient.fetch<any>("apikey")
expect(resp).toEqual({ test: "test" })
})

it("should allow setting a publishable key", async () => {
const pubClient = new Client({
baseUrl,
publishableKey: "test-pub-key",
})

const resp = await pubClient.fetch<any>("pubkey")
expect(resp).toEqual({ test: "test" })
})
})

describe("GET requests", () => {
it("should fire a simple GET request and get back a JSON response by default", async () => {
const resp = await client.fetch<{ test: string }>("test")
expect(resp).toEqual({ test: "test" })
})

it("should throw an exception if a non-2xx status is received", async () => {
const err: FetchError = await client.fetch<any>("throw").catch((e) => e)
expect(err.status).toEqual(500)
expect(err.message).toEqual("Internal Server Error")
})
})

describe("POST requests", () => {
it("should fire a simple POST request and get back a JSON response", async () => {
const resp = await client.fetch<any>("create", {
body: { test: "test" },
method: "POST",
})
expect(resp).toEqual({ test: "test" })
})
})

describe("DELETE requests", () => {
it("should fire a simple DELETE request and get back a JSON response", async () => {
const resp = await client.fetch<any>("delete/123", {
method: "DELETE",
})
expect(resp).toEqual({ test: "test" })
})
})
})
8 changes: 8 additions & 0 deletions packages/core/js-sdk/src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Client } from "../client"

export class Admin {
private client: Client
constructor(client: Client) {
this.client = client
}
}

0 comments on commit 507a54a

Please sign in to comment.