chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,6 @@
/dist
node_modules
.DS_store
.env*
.env
*.sql

View File

@@ -0,0 +1,143 @@
# @medusajs/cache-redis
## 1.9.1
### Patch Changes
- [#6865](https://github.com/medusajs/medusa/pull/6865) [`8fd1488938`](https://github.com/medusajs/medusa/commit/8fd148893850eb66c5eae00c4ca9391a80ea2eb9) Thanks [@adrien2p](https://github.com/adrien2p)! - chore: medusa shutdown
- Updated dependencies [[`1eeb1e9de3`](https://github.com/medusajs/medusa/commit/1eeb1e9de3e0b571735437b00968ee96e4aabad5), [`e944a627f0`](https://github.com/medusajs/medusa/commit/e944a627f074fb39a56f4bc7b3d6d315736ebf7c), [`dd35a4dbff`](https://github.com/medusajs/medusa/commit/dd35a4dbff10c86ea3c5f7f817c18b6e60d599e3), [`1bcb13f892`](https://github.com/medusajs/medusa/commit/1bcb13f892bc61db21b3fc6bdbce85f747aeec4c), [`82a176e30e`](https://github.com/medusajs/medusa/commit/82a176e30e47a7d11caaf31c3023bd8db588b465), [`528ef4ca90`](https://github.com/medusajs/medusa/commit/528ef4ca90bb2cf6173dccc9fd6a9f9932ff9b76), [`4b57c5d286`](https://github.com/medusajs/medusa/commit/4b57c5d286f9dc6e2098c67e9fecb0d93175b5a1), [`667c8609cc`](https://github.com/medusajs/medusa/commit/667c8609ccf3850f5df8cf784723a95bd0d6d2a6), [`8fd1488938`](https://github.com/medusajs/medusa/commit/8fd148893850eb66c5eae00c4ca9391a80ea2eb9), [`1c6ba4468e`](https://github.com/medusajs/medusa/commit/1c6ba4468eab1440931c88929affd5b4c593f377)]:
- @medusajs/modules-sdk@1.12.11
## 1.9.0
### Minor Changes
- [#5511](https://github.com/medusajs/medusa/pull/5511) [`80fe362f3`](https://github.com/medusajs/medusa/commit/80fe362f33cba69e52418b57e2e2d476923fc510) Thanks [@adrien2p](https://github.com/adrien2p)! - Integration tests fixes and ignore ttl 0 on cache modules
### Patch Changes
- Updated dependencies [[`c39bf69a5`](https://github.com/medusajs/medusa/commit/c39bf69a5e5cae75d7fa12aa6022b10903557a32), [`154c9b43b`](https://github.com/medusajs/medusa/commit/154c9b43bde1fdff562aba9da8a79af2660b29b3)]:
- @medusajs/modules-sdk@1.12.3
## 1.8.9
### Patch Changes
- [#5468](https://github.com/medusajs/medusa/pull/5468) [`a45da9215`](https://github.com/medusajs/medusa/commit/a45da9215d2a7834c368037726aaa3961caadaf9) Thanks [@adrien2p](https://github.com/adrien2p)! - fix(medusa, modules-sdk, modules): Module loading was missing the expected dependencies and remote query reference fix
- Updated dependencies [[`a45da9215`](https://github.com/medusajs/medusa/commit/a45da9215d2a7834c368037726aaa3961caadaf9)]:
- @medusajs/modules-sdk@1.12.2
## 1.8.8
### Patch Changes
- [#4276](https://github.com/medusajs/medusa/pull/4276) [`afd1b67f1`](https://github.com/medusajs/medusa/commit/afd1b67f1c7de8cf07fd9fcbdde599a37914e9b5) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Use caret range
- Updated dependencies [[`14c0f62f8`](https://github.com/medusajs/medusa/commit/14c0f62f84704a4c87beff3daaff60a52f5c88b8)]:
- @medusajs/modules-sdk@1.8.8
## 1.8.7
### Patch Changes
- Updated dependencies [[`e73c3e51c`](https://github.com/medusajs/medusa/commit/e73c3e51c9cd192eeae7a57b24b07bd466214145)]:
- @medusajs/modules-sdk@1.8.7
## 1.8.6
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.6
## 1.8.5
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.5
## 1.8.4
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.4
## 1.8.3
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.3
## 1.8.2
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.2
## 1.8.1
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.1
## 1.8.0
### Minor Changes
- [#3187](https://github.com/medusajs/medusa/pull/3187) [`f97b3d7cc`](https://github.com/medusajs/medusa/commit/f97b3d7ccee381d3491337ab5144bb44520382a7) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa, cache-redis, cache-inmemory): Added cache modules
### Patch Changes
- [#3685](https://github.com/medusajs/medusa/pull/3685) [`8ddb3952c`](https://github.com/medusajs/medusa/commit/8ddb3952c045e6c05c8d0f6922f0d4ba30cf3bd4) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Fix RC package versions
- [#3649](https://github.com/medusajs/medusa/pull/3649) [`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Export initialize method for all modules
- [#3531](https://github.com/medusajs/medusa/pull/3531) [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Remove dependency on @medusajs/medusa from Inventory and Stock-Location Modules
- Updated dependencies [[`8ddb3952c`](https://github.com/medusajs/medusa/commit/8ddb3952c045e6c05c8d0f6922f0d4ba30cf3bd4), [`55e94d0b4`](https://github.com/medusajs/medusa/commit/55e94d0b45776776639d3970d4264b8f5c5385dd), [`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73), [`bca1f80dd`](https://github.com/medusajs/medusa/commit/bca1f80dd501d878455e1ad4f5091cf20ef900ea), [`271844aed`](https://github.com/medusajs/medusa/commit/271844aedbe45c369e188b5d06458dbd6984cd39), [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def)]:
- @medusajs/modules-sdk@1.8.0
## 1.8.0-rc.3
### Patch Changes
- [#3649](https://github.com/medusajs/medusa/pull/3649) [`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Export initialize method for all modules
- Updated dependencies [[`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b)]:
- @medusajs/modules-sdk@0.1.0-rc.4
## 1.8.0-rc.2
### Patch Changes
- Updated dependencies [[`55e94d0b4`](https://github.com/medusajs/medusa/commit/55e94d0b45776776639d3970d4264b8f5c5385dd)]:
- @medusajs/modules-sdk@0.1.0-rc.3
## 1.8.0-rc.1
### Patch Changes
- chore: Fix RC package versions
- Updated dependencies []:
- @medusajs/modules-sdk@0.1.0-rc.2
## 1.8.0-rc.0
### Minor Changes
- [#3187](https://github.com/medusajs/medusa/pull/3187) [`f97b3d7cc`](https://github.com/medusajs/medusa/commit/f97b3d7ccee381d3491337ab5144bb44520382a7) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa, cache-redis, cache-inmemory): Added cache modules
### Patch Changes
- [#3531](https://github.com/medusajs/medusa/pull/3531) [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Remove dependency on @medusajs/medusa from Inventory and Stock-Location Modules
- Updated dependencies [[`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73), [`271844aed`](https://github.com/medusajs/medusa/commit/271844aedbe45c369e188b5d06458dbd6984cd39), [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def)]:
- @medusajs/modules-sdk@0.1.0-rc.0

View File

@@ -0,0 +1,26 @@
# Medusa Cache Redis
Use Redis as a Medusa cache store.
## Installation
```
yarn add @medusajs/cache-redis
```
## Options
```
{
ttl?: number // Time to keep data in cache (in seconds)
redisUrl?: string // Redis instance connection string
redisOptions?: RedisOptions // Redis client options
namespace?: string // Prefix for event keys (the default is `medusa:`)
}
```
### Other caching modules
- [Medusa Cache In-Memory](../cache-inmemory/README.md)

View File

@@ -0,0 +1,13 @@
module.exports = {
transform: {
"^.+\\.[jt]s?$": [
"ts-jest",
{
tsconfig: "tsconfig.json",
isolatedModules: true,
},
],
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`],
}

View File

@@ -0,0 +1,41 @@
{
"name": "@medusajs/cache-redis",
"version": "1.9.1",
"description": "Redis Cache Module for Medusa",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/cache-redis"
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=16"
},
"files": [
"dist"
],
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"@medusajs/types": "^1.11.16",
"cross-env": "^5.2.1",
"jest": "^29.6.3",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
"scripts": {
"watch": "tsc --build --watch",
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"build": "rimraf dist && tsc --build",
"test": "jest --passWithNoTests"
},
"dependencies": {
"@medusajs/modules-sdk": "^1.12.11",
"awilix": "^8.0.0",
"ioredis": "^5.3.1"
}
}

View File

@@ -0,0 +1,15 @@
import { ModuleExports } from "@medusajs/modules-sdk"
import Loader from "./loaders"
import { RedisCacheService } from "./services"
const service = RedisCacheService
const loaders = [Loader]
const moduleDefinition: ModuleExports = {
service,
loaders,
}
export default moduleDefinition
export * from "./initialize"
export * from "./types"

View File

@@ -0,0 +1,23 @@
import {
ExternalModuleDeclaration,
InternalModuleDeclaration,
MedusaModule,
Modules,
} from "@medusajs/modules-sdk"
import { ICacheService } from "@medusajs/types"
import { RedisCacheModuleOptions } from "../types"
export const initialize = async (
options?: RedisCacheModuleOptions | ExternalModuleDeclaration
): Promise<ICacheService> => {
const serviceKey = Modules.CACHE
const loaded = await MedusaModule.bootstrap<ICacheService>({
moduleKey: serviceKey,
defaultPath: "@medusajs/cache-redis",
declaration: options as
| InternalModuleDeclaration
| ExternalModuleDeclaration,
})
return loaded[serviceKey]
}

View File

@@ -0,0 +1,37 @@
import { LoaderOptions } from "@medusajs/modules-sdk"
import { asValue } from "awilix"
import Redis from "ioredis"
import { RedisCacheModuleOptions } from "../types"
export default async ({
container,
logger,
options,
}: LoaderOptions): Promise<void> => {
const { redisUrl, redisOptions } = options as RedisCacheModuleOptions
if (!redisUrl) {
throw Error(
"No `redisUrl` provided in `cacheService` module options. It is required for the Redis Cache Module."
)
}
const connection = new Redis(redisUrl, {
// Lazy connect to properly handle connection errors
lazyConnect: true,
...(redisOptions ?? {}),
})
try {
await connection.connect()
logger?.info(`Connection to Redis in module 'cache-redis' established`)
} catch (err) {
logger?.error(
`An error occurred while connecting to Redis in module 'cache-redis': ${err}`
)
}
container.register({
cacheRedisConnection: asValue(connection),
})
}

View File

@@ -0,0 +1,29 @@
import { RedisCacheService } from "../index"
const redisClientMock = {
set: jest.fn(),
get: jest.fn(),
}
describe("RedisCacheService", () => {
let cacheService
beforeEach(() => {
jest.clearAllMocks()
})
it("Underlying client methods are called", async () => {
cacheService = new RedisCacheService(
{
cacheRedisConnection: redisClientMock,
},
{}
)
await cacheService.set("test-key", "value")
expect(redisClientMock.set).toBeCalled()
await cacheService.get("test-key")
expect(redisClientMock.get).toBeCalled()
})
})

View File

@@ -0,0 +1 @@
export { default as RedisCacheService } from "./redis-cache"

View File

@@ -0,0 +1,98 @@
import { ICacheService } from "@medusajs/types"
import { Redis } from "ioredis"
import { RedisCacheModuleOptions } from "../types"
const DEFAULT_NAMESPACE = "medusa"
const DEFAULT_CACHE_TIME = 30 // 30 seconds
const EXPIRY_MODE = "EX" // "EX" stands for an expiry time in second
type InjectedDependencies = {
cacheRedisConnection: Redis
}
class RedisCacheService implements ICacheService {
protected readonly TTL: number
protected readonly redis: Redis
private readonly namespace: string
constructor(
{ cacheRedisConnection }: InjectedDependencies,
options: RedisCacheModuleOptions = {}
) {
this.redis = cacheRedisConnection
this.TTL = options.ttl ?? DEFAULT_CACHE_TIME
this.namespace = options.namespace || DEFAULT_NAMESPACE
}
__hooks = {
onApplicationShutdown: async () => {
this.redis.disconnect()
},
}
/**
* Set a key/value pair to the cache.
* If the ttl is 0 it will act like the value should not be cached at all.
* @param key
* @param data
* @param ttl
*/
async set(
key: string,
data: Record<string, unknown>,
ttl: number = this.TTL
): Promise<void> {
if (ttl === 0) {
return
}
await this.redis.set(
this.getCacheKey(key),
JSON.stringify(data),
EXPIRY_MODE,
ttl
)
}
/**
* Retrieve a cached value belonging to the given key.
* @param cacheKey
*/
async get<T>(cacheKey: string): Promise<T | null> {
cacheKey = this.getCacheKey(cacheKey)
try {
const cached = await this.redis.get(cacheKey)
if (cached) {
return JSON.parse(cached)
}
} catch (err) {
await this.redis.del(cacheKey)
}
return null
}
/**
* Invalidate cache for a specific key. a key can be either a specific key or more global such as "ps:*".
* @param key
*/
async invalidate(key: string): Promise<void> {
const keys = await this.redis.keys(this.getCacheKey(key))
const pipeline = this.redis.pipeline()
keys.forEach(function (key) {
pipeline.del(key)
})
await pipeline.exec()
}
/**
* Returns namespaced cache key
* @param key
*/
private getCacheKey(key: string) {
return this.namespace ? `${this.namespace}:${key}` : key
}
}
export default RedisCacheService

View File

@@ -0,0 +1,27 @@
import { RedisOptions } from "ioredis"
/**
* Module config type
*/
export type RedisCacheModuleOptions = {
/**
* Time to keep data in cache (in seconds)
*/
ttl?: number
/**
* Redis connection string
*/
redisUrl?: string
/**
* Redis client options
*/
redisOptions?: RedisOptions
/**
* Prefix for event keys
* @default `medusa:`
*/
namespace?: string
}

View File

@@ -0,0 +1,33 @@
{
"compilerOptions": {
"lib": [
"es5",
"es6",
"es2019"
],
"target": "es5",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true // to use ES5 specific tooling
},
"include": ["src"],
"exclude": [
"dist",
"./src/**/__tests__",
"./src/**/__mocks__",
"./src/**/__fixtures__",
"node_modules"
]
}

View File

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