breaking: move shared HTTP utils to the framework (#9402)

Fixes: FRMW-2728, FRMW-2729

After this PR gets merged the following middleware will be exported from the `@medusajs/framework/http` import path.

- applyParamsAsFilters
- clearFiltersByKey
- applyDefaultFilters
- setContext
- getQueryConfig
- httpCompression
- maybeApplyLinkFilter
- refetchEntities
- unlessPath
- validateBody
- validateQuery

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Harminder Virk
2024-10-03 09:42:00 +00:00
committed by GitHub
co-authored by Adrien de Peretti
parent 193f93464f
commit 48e00169d2
557 changed files with 2365 additions and 3499 deletions
@@ -2,21 +2,21 @@ import { SearchTypes } from "@medusajs/types"
/**
* ## Overview
*
*
* A search service class is in a TypeScript or JavaScript file created in the `src/services` directory. The class must extend the `AbstractSearchService` class imported
* from the `@medusajs/utils` package.
*
* Based on services naming conventions, the files name should be the slug version of the search services name without `service`, and the classs name should be the
*
* Based on services naming conventions, the files name should be the slug version of the search services name without `service`, and the classs name should be the
* pascal case of the search services name following by `Service`.
*
*
* For example, create the `MySearchService` class in the file `src/services/my-search.ts`:
*
*
* ```ts title="src/services/my-search.ts"
* import { AbstractSearchService } from "@medusajs/utils"
*
*
* class MySearchService extends AbstractSearchService {
* isDefault = false
*
*
* createIndex(indexName: string, options: Record<string, any>) {
* throw new Error("Method not implemented.")
* }
@@ -48,7 +48,7 @@ import { SearchTypes } from "@medusajs/types"
* }
* search(
* indexName: string,
* query: string,
* query: string,
* options: Record<string, any>
* ) {
* return {
@@ -56,24 +56,24 @@ import { SearchTypes } from "@medusajs/types"
* }
* }
* updateSettings(
* indexName: string,
* indexName: string,
* settings: Record<string, any>
* ) {
* throw new Error("Method not implemented.")
* }
*
*
* }
*
*
* export default MySearchService
* ```
*
*
* ---
*
*
* ## Notes About Class Methods
*
* Although there are several helper methods in this class, the main methods used by the Medusa backend are `addDocuments`, `deleteDocument`, and `search`.
*
* Although there are several helper methods in this class, the main methods used by the Medusa backend are `addDocuments`, `deleteDocument`, and `search`.
* The rest of the methods are provided in case you need them for custom use cases.
*
*
* ---
*/
export abstract class AbstractSearchService
@@ -122,15 +122,15 @@ export abstract class AbstractSearchService
* @example
* // ...
* import { ProductService } from "@medusajs/medusa"
*
*
* type InjectedDependencies = {
* productService: ProductService
* }
*
*
* class MySearchService extends AbstractSearchService {
* // ...
* protected readonly productService_: ProductService
*
*
* constructor({ productService }: InjectedDependencies) {
* // @ts-expect-error prefer-rest-params
* super(...arguments)
@@ -140,7 +140,7 @@ export abstract class AbstractSearchService
* // communicates with a third-party service.
* this.client = new Client(options)
* }
*
*
* // ...
* }
*/
@@ -150,15 +150,15 @@ export abstract class AbstractSearchService
/**
* This method is used to create an index in the search engine.
*
*
* @param {string} indexName - The name of the index to create.
* @param {unknown} options - Any options that may be relevant to your search service. This parameter doesn't have
* @param {unknown} options - Any options that may be relevant to your search service. This parameter doesn't have
* any defined format as it depends on your custom implementation.
* @returns {unknown} No required format of returned data, as it depends on your custom implementation.
*
*
* @example
* An example implementation, assuming `client` would interact with a third-party service:
*
*
* ```ts title="src/services/my-search.ts"
* class MySearchService extends AbstractSearchService {
* // ...
@@ -168,10 +168,10 @@ export abstract class AbstractSearchService
* // ...
* }
* ```
*
*
* Another example of how the [MeiliSearch plugin](https://docs.medusajs.com/plugins/search/meilisearch) uses the
* `options` parameter:
*
*
* ```ts
* class MeiliSearchService extends AbstractSearchService {
* // ...
@@ -189,14 +189,14 @@ export abstract class AbstractSearchService
/**
* This method is used to retrieve an indexs results from the search engine.
*
*
* @param {string} indexName - The name of the index
* @returns {unknown} No required format of returned data, as it depends on your custom implementation.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* getIndex(indexName: string) {
* return this.client_.getIndex(indexName)
* }
@@ -206,19 +206,19 @@ export abstract class AbstractSearchService
/**
* This method is used to add a document to an index in the search engine.
*
*
* When the Medusa backend loads, it triggers indexing for all products available in the Medusa backend, which uses this method to add or update documents.
* Its also used whenever a new product is added or a product is updated.
*
*
* @param {string} indexName - The name of the index to add the documents to.
* @param {unknown} documents - The list of documents to add. For example, an array of {@link entities!Product | products}.
* @param {string} type - The type of documents being indexed. For example, `products`.
* @returns {unknown} The response of saving the documents in the search engine, but theres no required format of the response.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* async addDocuments(
* indexName: string,
* documents: Record<string, any>[],
@@ -237,17 +237,17 @@ export abstract class AbstractSearchService
/**
* This method is used to replace existing documents in the search engine of an index with new documents.
*
*
* @param {string} indexName - The name of the index that the documents belong to.
* @param {unknown} documents - The list of documents to index. For example, it can be an array of {@link entities!Product | products}.
* Based on your search engine implementation, the documents should include an identification key that allows replacing the existing documents.
* @param {string} type - The type of documents being replaced. For example, `products`.
* @returns {unknown} The response of replacing the documents in the search engine, but theres no required format of the response.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* async replaceDocuments(
* indexName: string,
* documents: Record<string, any>[],
@@ -268,18 +268,18 @@ export abstract class AbstractSearchService
/**
* This method is used to delete a document from an index.
*
*
* When a product is deleted in the Medusa backend, this method is used to delete the product from the search engines index.
*
*
* @param {string} indexName - The name of the index that the document belongs to.
* @param {string | number} document_id - The ID of the item indexed. For example, if the deleted item is a product, then this is
* the ID of the product.
* @returns {unknown} The response of deleting the document in the search engine, but theres no required format of the response.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* async deleteDocument(
* indexName: string,
* document_id: string | number
@@ -296,14 +296,14 @@ export abstract class AbstractSearchService
/**
* This method is used to delete all documents from an index.
*
*
* @param {string} indexName - The index's name.
* @returns {unknown} The response of deleting the documents of that index in the search engine, but theres no required format of the response.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* async deleteAllDocuments(indexName: string) {
* return await this.client_
* .deleteDocuments(indexName)
@@ -314,26 +314,26 @@ export abstract class AbstractSearchService
/**
* This method is used to search through an index by a query.
*
* In the Medusa backend, this method is used within the [Search Products API Route](https://docs.medusajs.com/api/store#products_postproductssearch)
*
* In the Medusa backend, this method is used within the [Search Products API Route](https://docs.medusajs.com/api/store#products_postproductssearch)
* to retrieve the search results. The API route's response type is an array of items, though the item's format is not defined as it depends on the
* data returned by this method.
*
* @param {string} indexName - The index's name. In the case of the Search Products API Routes, its value is `products`.
* @param {string | null} query - The search query to retrieve results for.
* @param {unknown} options -
* @param {unknown} options -
* Options that can configure the search process. The Search Products API route passes an object having the properties:
*
*
* - `paginationOptions`: An object having an `offset` and `limit` properties, which are passed in the API Route's body.
* - `filter`: Filters that are passed in the API Route's request body. Its format is unknown, so you can pass filters based on your search service.
* - `additionalOptions`: Any other parameters that may be passed in the request's body.
*
*
* @returns {unknown} The list of results. For example, an array of products.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* async search(
* indexName: string,
* query: string,
@@ -355,18 +355,18 @@ export abstract class AbstractSearchService
/**
* This method is used to update the settings of an index within the search service. This is useful if you want to update the index settings when the plugin options change.
*
* For example, in the Algolia plugin, a loader, which runs when the Medusa backend loads, is used to update the settings of indices based on the plugin options.
*
* For example, in the Algolia plugin, a loader, which runs when the Medusa backend loads, is used to update the settings of indices based on the plugin options.
* The loader uses this method to update the settings.
*
*
* @param {string} indexName - The index's name to update its settings.
* @param {unknown} settings - The settings to update. Its format depends on your use case.
* @returns {unknown} The response of updating the index in the search engine, but theres no required format of the response.
*
*
* @example
* class MySearchService extends AbstractSearchService {
* // ...
*
*
* async updateSettings(
* indexName: string,
* settings: Record<string, any>