-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create the Medusa API SDK as js-sdk package
- Loading branch information
Showing
13 changed files
with
963 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.