-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add missing endpoints and normalize customer and currency endpo…
…ints for storefront (#7160)
- Loading branch information
Showing
24 changed files
with
368 additions
and
423 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
integration-tests/modules/__tests__/customer/store/list-customer-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 14 additions & 4 deletions
18
packages/medusa/src/api-v2/store/currencies/[code]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
import { remoteQueryObjectFromString } from "@medusajs/utils" | ||
import { | ||
ContainerRegistrationKeys, | ||
MedusaError, | ||
remoteQueryObjectFromString, | ||
} from "@medusajs/utils" | ||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" | ||
import { defaultStoreCurrencyFields } from "../query-config" | ||
|
||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { | ||
const remoteQuery = req.scope.resolve("remoteQuery") | ||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) | ||
|
||
const variables = { code: req.params.code } | ||
|
||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "currency", | ||
variables, | ||
fields: defaultStoreCurrencyFields, | ||
fields: req.remoteQueryConfig.fields, | ||
}) | ||
|
||
const [currency] = await remoteQuery(queryObject) | ||
if (!currency) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_FOUND, | ||
`Currency with code: ${req.params.code} was not found` | ||
) | ||
} | ||
|
||
res.status(200).json({ currency }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
packages/medusa/src/api-v2/store/currencies/query-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
export const defaultStoreCurrencyRelations = [] | ||
export const allowedStoreCurrencyRelations = [] | ||
export const defaultStoreCurrencyFields = [ | ||
"code", | ||
"name", | ||
"symbol", | ||
"symbol_native", | ||
"decimal_digits", | ||
"rounding", | ||
] | ||
|
||
export const retrieveTransformQueryConfig = { | ||
defaultFields: defaultStoreCurrencyFields, | ||
defaultRelations: defaultStoreCurrencyRelations, | ||
allowedRelations: allowedStoreCurrencyRelations, | ||
defaults: defaultStoreCurrencyFields, | ||
isList: false, | ||
} | ||
|
||
export const listTransformQueryConfig = { | ||
defaultLimit: 20, | ||
...retrieveTransformQueryConfig, | ||
defaultLimit: 50, | ||
isList: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 17 additions & 28 deletions
45
packages/medusa/src/api-v2/store/currencies/validators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,19 @@ | ||
import { Type } from "class-transformer" | ||
import { IsOptional, IsString, ValidateNested } from "class-validator" | ||
import { FindParams, extendedFindParamsMixin } from "../../../types/common" | ||
import { createFindParams, createSelectParams } from "../../utils/validators" | ||
import { z } from "zod" | ||
|
||
export class StoreGetCurrenciesCurrencyParams extends FindParams {} | ||
/** | ||
* Parameters used to filter and configure the pagination of the retrieved currencies. | ||
*/ | ||
export class StoreGetCurrenciesParams extends extendedFindParamsMixin({ | ||
limit: 50, | ||
offset: 0, | ||
}) { | ||
/** | ||
* Search parameter for currencies. | ||
*/ | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
code?: string | string[] | ||
|
||
// Additional filters from BaseFilterable | ||
@IsOptional() | ||
@ValidateNested({ each: true }) | ||
@Type(() => StoreGetCurrenciesParams) | ||
$and?: StoreGetCurrenciesParams[] | ||
export const StoreGetCurrencyParams = createSelectParams() | ||
|
||
@IsOptional() | ||
@ValidateNested({ each: true }) | ||
@Type(() => StoreGetCurrenciesParams) | ||
$or?: StoreGetCurrenciesParams[] | ||
} | ||
export type StoreGetCurrenciesParamsType = z.infer< | ||
typeof StoreGetCurrenciesParams | ||
> | ||
export const StoreGetCurrenciesParams = createFindParams({ | ||
offset: 0, | ||
limit: 50, | ||
}).merge( | ||
z.object({ | ||
q: z.string().optional(), | ||
code: z.union([z.string(), z.array(z.string())]).optional(), | ||
$and: z.lazy(() => StoreGetCurrenciesParams.array()).optional(), | ||
$or: z.lazy(() => StoreGetCurrenciesParams.array()).optional(), | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { MedusaContainer } from "@medusajs/types" | ||
import { | ||
ContainerRegistrationKeys, | ||
remoteQueryObjectFromString, | ||
} from "@medusajs/utils" | ||
|
||
export const refetchCustomer = async ( | ||
customerId: string, | ||
scope: MedusaContainer, | ||
fields: string[] | ||
) => { | ||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) | ||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "customer", | ||
variables: { | ||
filters: { id: customerId }, | ||
}, | ||
fields: fields, | ||
}) | ||
|
||
const customers = await remoteQuery(queryObject) | ||
return customers[0] | ||
} |
Oops, something went wrong.