diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 5b090195..b073a444 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: node-verion: [16.x] - medusajs-version: [1.2.1, 1.3.0] + medusajs-version: [1.2.x, 1.3.x] steps: - name: Cancel Previous Runs uses: styfle/cancel-workflow-action@0.9.1 @@ -43,7 +43,7 @@ jobs: strategy: matrix: node-verion: [16.x] - medusajs-version: [1.2.1, 1.3.0] + medusajs-version: [1.2.x, 1.3.x] services: postgres: image: postgres diff --git a/documentation/02-api-documentation.md b/documentation/02-api-documentation.md index ee3f94a2..6d86861b 100644 --- a/documentation/02-api-documentation.md +++ b/documentation/02-api-documentation.md @@ -70,6 +70,30 @@ export class Product extends MedusaProduct { > The `override` parameter of the `@MedusaEntity` decorator allow to specify which entity > from the core must be overridden. +To make your project aware of your customisation +at the medusa core level, you can use the [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) +approach. + +here is an example + +```ts +// src/modules/your_modules/index.d.ts + +import { User as ExtendedUser } from '@modules/user/user.entity'; +import { default as ExtendedUserRepository } from '@modules/user/user.repository'; + +declare module '@medusajs/medusa/dist/models/user' { + export declare class User extends ExtendedUser {} +} + +declare module '@medusajs/medusa' { + export declare class UserRepository extends ExtendedUserRepository {} +} + +``` + +To see a live example, you can look at the starters, they include the module augmentation approach. + ### @Repository This decorator gives you the ability to either create a new custom repository that will diff --git a/jest.config.js b/jest.config.js index f4d2c7b6..0e71a229 100644 --- a/jest.config.js +++ b/jest.config.js @@ -20,5 +20,6 @@ module.exports = { "lcov" ], "coverageDirectory": "../coverage", - "testEnvironment": "node" + "testEnvironment": "node", + "setupFilesAfterEnv": ["/../setupTests.js"] }; diff --git a/setupTests.js b/setupTests.js new file mode 100644 index 00000000..592165a9 --- /dev/null +++ b/setupTests.js @@ -0,0 +1,3 @@ +global.afterEach(async () => { + await new Promise(resolve => setImmediate(resolve)) +}) diff --git a/starters/plugin-module/src/modules/user/index.d.ts b/starters/plugin-module/src/modules/user/index.d.ts new file mode 100644 index 00000000..1e95804e --- /dev/null +++ b/starters/plugin-module/src/modules/user/index.d.ts @@ -0,0 +1,10 @@ +import { User as ExtendedUser } from '@modules/user/user.entity'; +import { default as ExtendedUserRepository } from '@modules/user/user.repository'; + +declare module '@medusajs/medusa/dist/models/user' { + export declare class User extends ExtendedUser {} +} + +declare module '@medusajs/medusa' { + export declare class UserRepository extends ExtendedUserRepository {} +} diff --git a/starters/plugin-module/src/modules/user/user.entity.ts b/starters/plugin-module/src/modules/user/user.entity.ts index 461fdf40..98a0e6cf 100644 --- a/starters/plugin-module/src/modules/user/user.entity.ts +++ b/starters/plugin-module/src/modules/user/user.entity.ts @@ -1,4 +1,4 @@ -import { User as MedusaUser } from '@medusajs/medusa/dist'; +import { User as MedusaUser } from '@medusajs/medusa'; import { Column, Entity, Index } from 'typeorm'; import { Entity as MedusaEntity } from 'medusa-extender'; diff --git a/starters/plugin-module/src/modules/user/user.service.ts b/starters/plugin-module/src/modules/user/user.service.ts index 474686a4..b29263f0 100644 --- a/starters/plugin-module/src/modules/user/user.service.ts +++ b/starters/plugin-module/src/modules/user/user.service.ts @@ -1,7 +1,9 @@ import { Service } from 'medusa-extender'; import { EntityManager } from 'typeorm'; import { EventBusService, UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; -import UserRepository from './user.repository'; +import { FindConfig } from '@medusajs/medusa/dist/types/common'; +import { User } from '@medusajs/medusa'; +import UserRepository from '@modules/user/user.repository'; type InjectedDependencies = { manager: EntityManager; @@ -19,4 +21,8 @@ export default class UserService extends MedusaUserService { this.manager = manager; this.userRepository = userRepository; } + + async retrieve(userId: string, config?: FindConfig): Promise { + return await super.retrieve(userId, config); + } } diff --git a/starters/server/src/modules/user/index.d.ts b/starters/server/src/modules/user/index.d.ts new file mode 100644 index 00000000..fad50ed5 --- /dev/null +++ b/starters/server/src/modules/user/index.d.ts @@ -0,0 +1,12 @@ +import { User as MedusaUser } from '@medusajs/medusa/dist/models/user'; +import { default as ExtendedUserRepository } from '@modules/user/user.repository'; + +declare module '@medusajs/medusa/dist/models/user' { + export declare class User extends MedusaUser { + store_id: string; + } +} + +declare module '@medusajs/medusa' { + export declare class UserRepository extends ExtendedUserRepository {} +} diff --git a/starters/server/src/modules/user/user.entity.ts b/starters/server/src/modules/user/user.entity.ts index 461fdf40..98a0e6cf 100644 --- a/starters/server/src/modules/user/user.entity.ts +++ b/starters/server/src/modules/user/user.entity.ts @@ -1,4 +1,4 @@ -import { User as MedusaUser } from '@medusajs/medusa/dist'; +import { User as MedusaUser } from '@medusajs/medusa'; import { Column, Entity, Index } from 'typeorm'; import { Entity as MedusaEntity } from 'medusa-extender'; diff --git a/starters/server/src/modules/user/user.service.ts b/starters/server/src/modules/user/user.service.ts index 474686a4..b4063f36 100644 --- a/starters/server/src/modules/user/user.service.ts +++ b/starters/server/src/modules/user/user.service.ts @@ -2,6 +2,8 @@ import { Service } from 'medusa-extender'; import { EntityManager } from 'typeorm'; import { EventBusService, UserService as MedusaUserService } from '@medusajs/medusa/dist/services'; import UserRepository from './user.repository'; +import { FindConfig } from '@medusajs/medusa/dist/types/common'; +import { User } from '@medusajs/medusa'; type InjectedDependencies = { manager: EntityManager; @@ -19,4 +21,8 @@ export default class UserService extends MedusaUserService { this.manager = manager; this.userRepository = userRepository; } + + async retrieve(userId: string, config?: FindConfig): Promise { + return await super.retrieve(userId, config); + } }