Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CommonJS support for js-sdk and fix TypeScript definitions for better IDE support in platforms like Nest.js #9229

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions packages/core/js-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"name": "@medusajs/js-sdk",
"version": "0.0.1",
"version": "0.0.2",
"description": "SDK for the Medusa API",
"main": "dist/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"exports": {
"import": "./dist/esm/index.js",
"require": "./dist/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"url": "git+https://github.com/medusajs/medusa.git",
"directory": "packages/core/js-sdk"
},
"engines": {
"node": ">=18"
"node": ">=20"
},
"publishConfig": {
"access": "public"
Expand All @@ -20,19 +26,21 @@
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"@medusajs/types": "^1.11.16",
"cross-env": "^5.2.1",
"jest": "^29.7.0",
"msw": "^2.3.0",
"rimraf": "^5.0.1",
"typescript": "^5.1.6"
"@types/jest": "29.5.13",
"cross-env": "7.0.3",
"jest": "29.7.0",
"msw": "2.4.9",
"rimraf": "6.0.1",
"typescript": "5.6.2"
},
"dependencies": {
"fetch-event-stream": "^0.1.5",
"qs": "^6.12.1"
"@medusajs/client-types": "0.2.12",
"@medusajs/types": "1.12.0-preview-20240918180312",
"fetch-event-stream": "0.1.5",
"qs": "6.13.0"
},
"scripts": {
"build": "rimraf dist && tsc --build",
"build": "rimraf dist && tsc -p tsconfig.json && tsc -p tsconfig.esm.json",
"test": "jest --passWithNoTests --runInBand --bail --forceExit",
"watch": "tsc --build --watch"
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/js-sdk/src/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const server = setupServer(
}
}),
http.get(`${baseUrl}/replaced-header`, ({ request, params, cookies }) => {
request.headers
if (request.headers.get("Content-Type") === "application/xml") {
return HttpResponse.json({
test: "test",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/js-sdk/src/admin/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
HttpTypes,
PaginatedResponse,
SelectParams,
AdminOrderChangesResponse
} from "@medusajs/types"
import { AdminOrderChangesResponse } from "@medusajs/types/src/http/order/admin/responses"

import { Client } from "../client"
import { ClientHeaders } from "../types"
Expand Down
23 changes: 17 additions & 6 deletions packages/core/js-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,27 @@ const normalizeRequest = (
if (body && headers.get("content-type")?.includes("application/json")) {
body = JSON.stringify(body)
}

return {
let returnData = {
...init,
headers,
// TODO: Setting this to "include" poses some security risks, as it will send cookies to any domain. We should consider making this configurable.
credentials: config.auth?.type === "session" ? "include" : "omit",
...(body ? { body: body as RequestInit["body"] } : {}),
} as RequestInit
} as RequestInit;

if(returnData.headers) {
for (const [key, value] of Object.entries(returnData.headers)) {
headers.set(key, value);
}
}

returnData.headers = {};

for(const [key, value] of headers.entries()) {
returnData.headers[key] = value;
}

return returnData;
}

const normalizeResponse = async (resp: Response, reqHeaders: Headers) => {
Expand Down Expand Up @@ -291,8 +304,6 @@ export class Client {
return this.token
}
}

return
}

protected getTokenStorageInfo_ = () => {
Expand Down
34 changes: 34 additions & 0 deletions packages/core/js-sdk/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"compilerOptions": {
"lib": [
"ESNext",
"DOM",
"DOM.Iterable"
],
"target": "ES6",
"outDir": "./dist/esm",
"esModuleInterop": true,
"declarationMap": true,
"declaration": true,
"module": "ESNext",
"moduleResolution": "Bundler",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true
},
"include": [
"./src/**/*"
],
"exclude": [
"./dist/**/*",
"./src/**/__tests__",
"./src/**/__mocks__",
"node_modules"
]
}
14 changes: 10 additions & 4 deletions packages/core/js-sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"compilerOptions": {
"lib": ["es2021"],
"target": "es2021",
"lib": [
"ESNext",
"DOM",
"DOM.Iterable"
],
"target": "ES2021",
"outDir": "./dist",
"esModuleInterop": true,
"declarationMap": true,
"declaration": true,
"module": "ES2020",
"module": "CommonJS",
"moduleResolution": "Node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand All @@ -18,7 +22,9 @@
"allowJs": true,
"skipLibCheck": true
},
"include": ["./src/**/*"],
"include": [
"./src/**/*"
],
"exclude": [
"./dist/**/*",
"./src/**/__tests__",
Expand Down