Skip to content

Commit

Permalink
feat: Integrate module augmentation in the doc and starters (#104)
Browse files Browse the repository at this point in the history
* feat: Integrate module augmentation in the doc and starters

* chore: update action

* test: fix suite

* test: fix config

* docs: Improve augmentation
  • Loading branch information
adrien2p authored Jul 27, 2022
1 parent f913ae5 commit 17915cd
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions documentation/02-api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ module.exports = {
"lcov"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"setupFilesAfterEnv": ["<rootDir>/../setupTests.js"]
};
3 changes: 3 additions & 0 deletions setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global.afterEach(async () => {
await new Promise(resolve => setImmediate(resolve))
})
10 changes: 10 additions & 0 deletions starters/plugin-module/src/modules/user/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 {}
}
2 changes: 1 addition & 1 deletion starters/plugin-module/src/modules/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
8 changes: 7 additions & 1 deletion starters/plugin-module/src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,4 +21,8 @@ export default class UserService extends MedusaUserService {
this.manager = manager;
this.userRepository = userRepository;
}

async retrieve(userId: string, config?: FindConfig<User>): Promise<User> {
return await super.retrieve(userId, config);
}
}
12 changes: 12 additions & 0 deletions starters/server/src/modules/user/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 {}
}
2 changes: 1 addition & 1 deletion starters/server/src/modules/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
6 changes: 6 additions & 0 deletions starters/server/src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,4 +21,8 @@ export default class UserService extends MedusaUserService {
this.manager = manager;
this.userRepository = userRepository;
}

async retrieve(userId: string, config?: FindConfig<User>): Promise<User> {
return await super.retrieve(userId, config);
}
}

0 comments on commit 17915cd

Please sign in to comment.