chore(medusa,types): [1] Add request types to API routes (#8558)

Add request types as type arguments in API routes. This also includes adding / rearranging types under the `types` package.

PR 1/n
This commit is contained in:
Shahed Nasser
2024-08-12 14:27:00 +03:00
committed by GitHub
parent dd4147314f
commit 24985cf89d
19 changed files with 86 additions and 30 deletions

View File

@@ -0,0 +1,11 @@
import {
BaseCart,
BaseCartAddress,
BaseCartLineItem,
BaseCartShippingMethod,
} from "../common"
export interface StoreCart extends BaseCart {}
export interface StoreCartLineItem extends BaseCartLineItem {}
export interface StoreCartAddress extends BaseCartAddress {}
export interface StoreCartShippingMethod extends BaseCartShippingMethod {}

View File

@@ -0,0 +1,3 @@
export * from "./entities"
export * from "./payloads"
export * from "./responses"

View File

@@ -1,14 +1,4 @@
import {
BaseCart,
BaseCartAddress,
BaseCartLineItem,
BaseCartShippingMethod,
} from "./common"
export interface StoreCart extends BaseCart {}
export interface StoreCartLineItem extends BaseCartLineItem {}
export interface StoreCartAddress extends BaseCartAddress {}
export interface StoreCartShippingMethod extends BaseCartShippingMethod {}
import { StoreCartAddress, StoreCartLineItem } from "./entities"
export interface StoreCreateCart {
region_id?: string

View File

@@ -0,0 +1,19 @@
import { StoreOrder } from "../../order"
import { StoreCart } from "./entities"
export interface StoreCartResponse {
cart: StoreCart
}
export type StoreCompleteCartResponse = {
type: "cart"
cart: StoreCart
error: {
message: string
name: string
type: string
}
} | {
type: "order"
order: StoreOrder
}

View File

@@ -1,4 +0,0 @@
import { BaseCollection, BaseCollectionListParams } from "./common"
export interface StoreCollection extends BaseCollection {}
export interface StoreCollectionFilters extends BaseCollectionListParams {}

View File

@@ -0,0 +1,3 @@
import { BaseCollection, BaseCollectionListParams } from "../common"
export interface StoreCollection extends BaseCollection {}

View File

@@ -0,0 +1,3 @@
export * from "./entities"
export * from "./queries"
export * from "./responses"

View File

@@ -0,0 +1,3 @@
import { BaseCollectionListParams } from "../common";
export interface StoreCollectionFilters extends BaseCollectionListParams {}

View File

@@ -0,0 +1,10 @@
import { StoreCollection } from "."
import { PaginatedResponse } from "../../common"
export interface StoreCollectionResponse {
collection: StoreCollection
}
export type StoreCollectionListResponse = PaginatedResponse<{
collections: StoreCollection[]
}>

View File

@@ -6,10 +6,11 @@ import { refetchOrder } from "../../../orders/helpers"
import { refetchCart } from "../../helpers"
import { defaultStoreCartFields } from "../../query-config"
import { StoreCompleteCartType } from "../../validators"
import { HttpTypes } from "@medusajs/types"
export const POST = async (
req: MedusaRequest<StoreCompleteCartType>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCompleteCartResponse>
) => {
const cart_id = req.params.id

View File

@@ -2,7 +2,7 @@ import {
deleteLineItemsWorkflow,
updateLineItemInCartWorkflow,
} from "@medusajs/core-flows"
import { DeleteResponse } from "@medusajs/types"
import { DeleteResponse, HttpTypes } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
import { prepareListQuery } from "../../../../../../utils/get-query-config"
@@ -11,7 +11,7 @@ import { StoreUpdateCartLineItemType } from "../../../validators"
export const POST = async (
req: MedusaRequest<StoreUpdateCartLineItemType>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const cart = await refetchCart(
req.params.id,

View File

@@ -2,10 +2,11 @@ import { addToCartWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { refetchCart } from "../../helpers"
import { StoreAddCartLineItemType } from "../../validators"
import { HttpTypes } from "@medusajs/types"
export const POST = async (
req: MedusaRequest<StoreAddCartLineItemType>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const cart = await refetchCart(
req.params.id,

View File

@@ -6,10 +6,11 @@ import {
StoreAddCartPromotionsType,
StoreRemoveCartPromotionsType,
} from "../../validators"
import { HttpTypes } from "@medusajs/types"
export const POST = async (
req: MedusaRequest<StoreAddCartPromotionsType>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const workflow = updateCartPromotionsWorkflow(req.scope)
const payload = req.validatedBody
@@ -33,7 +34,9 @@ export const POST = async (
export const DELETE = async (
req: MedusaRequest<StoreRemoveCartPromotionsType>,
res: MedusaResponse
res: MedusaResponse<{
cart: HttpTypes.StoreCart
}>
) => {
const workflow = updateCartPromotionsWorkflow(req.scope)
const payload = req.validatedBody

View File

@@ -1,10 +1,13 @@
import { updateCartWorkflow } from "@medusajs/core-flows"
import { AdditionalData, UpdateCartDataDTO } from "@medusajs/types"
import { AdditionalData, HttpTypes, UpdateCartDataDTO } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { refetchCart } from "../helpers"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
export const GET = async (
req: MedusaRequest,
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -16,7 +19,9 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
export const POST = async (
req: MedusaRequest<UpdateCartDataDTO & AdditionalData>,
res: MedusaResponse
res: MedusaResponse<{
cart: HttpTypes.StoreCart
}>
) => {
const workflow = updateCartWorkflow(req.scope)

View File

@@ -2,10 +2,11 @@ import { addShippingMethodToWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { refetchCart } from "../../helpers"
import { StoreAddCartShippingMethodsType } from "../../validators"
import { HttpTypes } from "@medusajs/types"
export const POST = async (
req: MedusaRequest<StoreAddCartShippingMethodsType>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const workflow = addShippingMethodToWorkflow(req.scope)
const payload = req.validatedBody

View File

@@ -2,10 +2,11 @@ import { updateTaxLinesWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { refetchCart } from "../../helpers"
import { StoreCalculateCartTaxesType } from "../../validators"
import { HttpTypes } from "@medusajs/types"
export const POST = async (
req: MedusaRequest<StoreCalculateCartTaxesType>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const workflow = updateTaxLinesWorkflow(req.scope)

View File

@@ -1,5 +1,9 @@
import { createCartWorkflow } from "@medusajs/core-flows"
import { AdditionalData, CreateCartWorkflowInputDTO } from "@medusajs/types"
import {
AdditionalData,
CreateCartWorkflowInputDTO,
HttpTypes,
} from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
@@ -8,7 +12,7 @@ import { refetchCart } from "./helpers"
export const POST = async (
req: AuthenticatedMedusaRequest<CreateCartWorkflowInputDTO & AdditionalData>,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const workflowInput = {
...req.validatedBody,

View File

@@ -1,3 +1,4 @@
import { HttpTypes } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
@@ -6,7 +7,7 @@ import { refetchCollection } from "../helpers"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCollectionResponse>
) => {
const collection = await refetchCollection(
req.params.id,

View File

@@ -1,3 +1,4 @@
import { HttpTypes } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
@@ -10,7 +11,7 @@ import {
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
res: MedusaResponse<HttpTypes.StoreCollectionListResponse>
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)