Przeglądaj źródła

chore(): Add typescript support

adrien2p 2 lat temu
rodzic
commit
a3234b2bea

+ 3 - 3
.babelrc.js

@@ -1,12 +1,12 @@
-let ignore = [`**/dist`];
+let ignore = [`**/dist`]
 
 // Jest needs to compile this code, but generally we don't want this copied
 // to output folders
 if (process.env.NODE_ENV !== `test`) {
-  ignore.push(`**/__tests__`);
+  ignore.push(`**/__tests__`)
 }
 
 module.exports = {
   presets: [["babel-preset-medusa-package"], ["@babel/preset-typescript"]],
   ignore,
-};
+}

+ 10 - 1
.gitignore

@@ -4,4 +4,13 @@
 /uploads
 /node_modules
 yarn-error.log
-/.idea
+
+.idea
+
+coverage
+
+!src/**
+
+./tsconfig.tsbuildinfo
+package-lock.json
+yarn.lock

+ 22 - 0
medusa-config.js

@@ -60,10 +60,32 @@ module.exports = {
     // For more production-like environment install PostgresQL
     // database_url: DATABASE_URL,
     // database_type: "postgres",
+		jwtSecret: process.env.JWT_SECRET,
+    cookieSecret: process.env.COOKIE_SECRET,
     database_database: "./medusa-db.sql",
     database_type: "sqlite",
     store_cors: STORE_CORS,
     admin_cors: ADMIN_CORS,
   },
   plugins,
+	modules: {
+    eventBus: {
+      resolve: "@medusajs/event-bus-redis",
+      options: {
+        redisUrl: REDIS_URL
+      }
+    },
+    cacheService: {
+      resolve: "@medusajs/cache-redis",
+      options: {
+        redisUrl: REDIS_URL
+      }
+    },
+		/*inventoryService: {
+      resolve: '@medusajs/inventory'
+    } ,
+    stockLocationService: {
+      resolve: '@medusajs/stock-location'
+    },*/
+  }
 };

+ 73 - 16
package.json

@@ -4,31 +4,88 @@
   "description": "A starter for Medusa projects.",
   "author": "Sebastian Rindom <skrindom@gmail.com>",
   "license": "MIT",
-  "scripts": {
-    "seed": "medusa seed -f ./data/seed.json",
-    "build": "babel src -d dist --extensions \".ts,.js\"",
-    "start": "medusa develop"
-  },
-  "dependencies": {
-    "@medusajs/medusa": "^1.3.4",
-    "@medusajs/medusa-cli": "^1.3.1",
-    "medusa-fulfillment-manual": "^1.1.31",
-    "medusa-interfaces": "^1.3.1",
-    "medusa-payment-manual": "^1.0.16",
-    "medusa-payment-stripe": "^1.1.41",
-    "typeorm": "^0.2.36"
-  },
   "repository": "https://github.com/medusajs/medusa-starter-default.git",
+  "contributors": [
+    {
+      "name": "Adrien de Peretti",
+      "email": "adrien.deperetti@gmail.com",
+      "url": "http://www.github.com/adrien2p"
+    }
+  ],
   "keywords": [
     "sqlite",
+    "postgres",
+    "typescript",
     "ecommerce",
     "headless",
     "medusa"
   ],
+  "scripts": {
+    "clean": "./node_modules/.bin/rimraf dist",
+    "build": "npm run clean && tsc -p tsconfig.json",
+    "watch": "tsc --watch",
+    "test": "jest",
+    "seed": "medusa seed -f ./data/seed.json",
+    "start": "npm run build && medusa start"
+  },
+  "dependencies": {
+    "@babel/preset-typescript": "^7.21.4",
+    "@medusajs/cache-redis": "^2.0.0-next-20230323083446",
+    "@medusajs/event-bus-redis": "^1.8.0-rc.1",
+    "@medusajs/medusa": "1.8.0-rc.5",
+    "@medusajs/medusa-cli": "^1.3.9-rc.1",
+    "@medusajs/types": "^0.0.2-rc.0",
+    "@medusajs/utils": "^0.0.2-rc.0",
+    "babel-preset-medusa-package": "^1.1.13",
+    "body-parser": "^1.19.0",
+    "cors": "^2.8.5",
+    "express": "^4.17.2",
+    "medusa-fulfillment-manual": "^2.0.0-next-20230322094312",
+    "medusa-interfaces": "^1.4.0-next.0",
+    "medusa-payment-manual": "^2.0.0-next-20230322094312",
+    "medusa-payment-stripe": "^2.0.0-snapshot-20230320172940",
+    "typeorm": "^0.3.11"
+  },
   "devDependencies": {
     "@babel/cli": "^7.14.3",
     "@babel/core": "^7.14.3",
-    "@babel/preset-typescript": "^7.14.5",
-    "babel-preset-medusa-package": "^1.1.19"
+    "@types/express": "^4.17.13",
+    "@types/jest": "^27.4.0",
+    "@types/node": "^17.0.8",
+    "babel-preset-medusa-package": "^1.1.13",
+    "cross-env": "^5.2.1",
+    "eslint": "^6.8.0",
+    "jest": "^27.3.1",
+    "mongoose": "^5.13.14",
+    "rimraf": "^3.0.2",
+    "ts-jest": "^27.0.7",
+    "ts-loader": "^9.2.6",
+    "typescript": "^4.5.2"
+  },
+  "jest": {
+    "globals": {
+      "ts-jest": {
+        "tsconfig": "tsconfig.spec.json"
+      }
+    },
+    "moduleFileExtensions": [
+      "js",
+      "json",
+      "ts"
+    ],
+    "testPathIgnorePatterns": [
+      "/node_modules/",
+      "<rootDir>/node_modules/"
+    ],
+    "rootDir": "src",
+    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|js)$",
+    "transform": {
+      ".ts": "ts-jest"
+    },
+    "collectCoverageFrom": [
+      "**/*.(t|j)s"
+    ],
+    "coverageDirectory": "./coverage",
+    "testEnvironment": "node"
   }
 }

+ 20 - 0
src/api/index.ts

@@ -0,0 +1,20 @@
+import { Router } from "express"
+import { getConfigFile } from "medusa-core-utils"
+import { getStoreRouter } from "./routes/store"
+import { ConfigModule } from "@medusajs/medusa/dist/types/global";
+
+export default (rootDirectory: string): Router | Router[] => {
+  const { configModule: {projectConfig} } = getConfigFile(
+    rootDirectory,
+    "medusa-config"
+  ) as { configModule: ConfigModule }
+
+  const storeCorsOptions = {
+    origin: projectConfig.store_cors.split(","),
+    credentials: true,
+  }
+
+  const storeRouter = getStoreRouter(storeCorsOptions)
+
+  return [storeRouter]
+}

+ 5 - 0
src/api/routes/store/custom-route-handler-2.ts

@@ -0,0 +1,5 @@
+import { Request, Response } from 'express'
+
+export default async (req: Request, res: Response): Promise<void> => {
+  res.sendStatus(200)
+}

+ 5 - 0
src/api/routes/store/custom-route-handler-3.ts

@@ -0,0 +1,5 @@
+import { Request, Response } from 'express'
+
+export default async (req: Request, res: Response): Promise<void> => {
+  res.sendStatus(200)
+}

+ 5 - 0
src/api/routes/store/custom-route-handler.ts

@@ -0,0 +1,5 @@
+import { Request, Response } from 'express'
+
+export default async (req: Request, res: Response): Promise<void> => {
+  res.sendStatus(200)
+}

+ 32 - 0
src/api/routes/store/index.ts

@@ -0,0 +1,32 @@
+import * as cors from "cors"
+import { Router } from "express"
+import * as bodyParser from "body-parser"
+import customRouteHandler from "./custom-route-handler"
+import customRouteHandler2 from "./custom-route-handler-2"
+import customRouteHandler3 from "./custom-route-handler-3"
+import { wrapHandler } from "@medusajs/utils";
+import { authenticateCustomer } from "@medusajs/medusa/dist/api/middlewares";
+
+const storeRouter = Router()
+export function getStoreRouter(storeCorsOptions): Router {
+  storeRouter.use(cors(storeCorsOptions), bodyParser.json())
+
+  storeRouter.options("/store/my-custom-path")
+  storeRouter.post(
+    "/store/my-custom-path",
+    wrapHandler(customRouteHandler)
+  )
+
+  storeRouter.post(
+    "/store/my-custom-path-2",
+    wrapHandler(customRouteHandler2)
+  )
+
+  storeRouter.post(
+    "/store/my-custom-protected-path",
+    authenticateCustomer(),
+    wrapHandler(customRouteHandler3)
+  )
+
+  return storeRouter
+}

+ 5 - 0
src/loaders/loader.ts

@@ -0,0 +1,5 @@
+import { AwilixContainer } from 'awilix'
+
+export default (container: AwilixContainer, config: Record<string, unknown>): void | Promise<void> => {
+  /* Implement your own loader. */
+}

+ 11 - 0
src/migrations/1617703530229-my-migration.ts

@@ -0,0 +1,11 @@
+import { MigrationInterface, QueryRunner } from "typeorm"
+
+export class MyMigration1617703530229 implements MigrationInterface {
+  name = "myMigration1617703530229"
+
+  public async up(queryRunner: QueryRunner): Promise<void> {
+  }
+
+  public async down(queryRunner: QueryRunner): Promise<void> {
+  }
+}

+ 17 - 0
src/models/custom.ts

@@ -0,0 +1,17 @@
+import { generateEntityId } from "@medusajs/utils"
+import {
+    BeforeInsert,
+    Entity,
+    PrimaryColumn,
+} from "typeorm"
+
+@Entity()
+export class MyModel {
+    @PrimaryColumn()
+    id: string
+
+    @BeforeInsert()
+    beforeInsert() {
+        this.id = generateEntityId(this.id, "mm_")
+    }
+}

+ 5 - 0
src/services/__tests__/test-service.spec.ts

@@ -0,0 +1,5 @@
+describe('MyService', () => {
+    it('should do this', async () => {
+        expect(true).toBe(true)
+    })
+})

+ 18 - 0
src/services/my-custom.ts

@@ -0,0 +1,18 @@
+import { Lifetime } from "awilix"
+import { TransactionBaseService } from "@medusajs/utils";
+import { IEventBusService } from "@medusajs/types";
+
+export default class MyCustomService extends TransactionBaseService {
+  static LIFE_TIME = Lifetime.SCOPED
+  protected readonly eventBusService_: IEventBusService
+
+  constructor(
+      { eventBusService }: { eventBusService: IEventBusService },
+      options: Record<string, unknown>
+  ) {
+    // @ts-ignore
+    super(...arguments)
+
+    this.eventBusService_ = eventBusService
+  }
+}

+ 29 - 0
src/subscribers/my-subscriber.ts

@@ -0,0 +1,29 @@
+import MyCustomService from "../services/my-custom";
+import { EntityManager } from "typeorm";
+import { EventBusService, OrderService } from "@medusajs/medusa";
+
+export default class MySubscriber {
+  protected readonly manager_: EntityManager;
+  protected readonly myCustomService_: MyCustomService
+
+  constructor(
+    {
+      manager,
+      eventBusService,
+      myCustomService,
+    }: {
+      manager: EntityManager;
+      eventBusService: EventBusService;
+      myCustomService: MyCustomService;
+    }
+  ) {
+    this.manager_ = manager;
+    this.myCustomService_ = myCustomService;
+
+    eventBusService.subscribe(OrderService.Events.PLACED, this.handleOrderPlaced);
+  }
+
+  handleOrderPlaced = async (data): Promise<any> => {
+    return true;
+  }
+}

+ 25 - 0
tsconfig.json

@@ -0,0 +1,25 @@
+{
+  "compilerOptions": {
+    "lib": ["es5", "es6"],
+    "target": "es5",
+    "allowJs": true,
+    "esModuleInterop": false,
+    "module": "commonjs",
+    "moduleResolution": "node",
+    "emitDecoratorMetadata": true,
+    "experimentalDecorators": true,
+    "skipLibCheck": true,
+    "skipDefaultLibCheck": true,
+    "declaration": false,
+    "sourceMap": false,
+    "outDir": "./dist",
+    "rootDir": "src",
+    "baseUrl": "src"
+  },
+  "include": ["src"],
+  "exclude": [
+    "**/__tests__",
+    "**/__fixtures__",
+    "node_modules"
+  ]
+}

+ 5 - 0
tsconfig.spec.json

@@ -0,0 +1,5 @@
+{
+  "extends": "./tsconfig.json",
+  "include": ["src"],
+  "exclude": ["dist", "node_modules"]
+}

Plik diff jest za duży
+ 539 - 158
yarn.lock


Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików