-
-
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 collection endpoints to store
- Loading branch information
Showing
10 changed files
with
245 additions
and
142 deletions.
There are no files selected for viewing
52 changes: 0 additions & 52 deletions
52
integration-tests/api/__tests__/store/__snapshots__/collections.js.snap
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,92 @@ | ||
import { medusaIntegrationTestRunner } from "medusa-test-utils" | ||
import { | ||
createAdminUser, | ||
adminHeaders, | ||
} from "../../../helpers/create-admin-user" | ||
|
||
jest.setTimeout(30000) | ||
|
||
medusaIntegrationTestRunner({ | ||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true }, | ||
testSuite: ({ dbConnection, getContainer, api }) => { | ||
let baseCollection | ||
let baseCollection1 | ||
let baseCollection2 | ||
|
||
beforeEach(async () => { | ||
const container = getContainer() | ||
await createAdminUser(dbConnection, adminHeaders, container) | ||
|
||
baseCollection = ( | ||
await api.post( | ||
"/admin/collections", | ||
{ title: "test-collection" }, | ||
adminHeaders | ||
) | ||
).data.collection | ||
|
||
baseCollection1 = ( | ||
await api.post( | ||
"/admin/collections", | ||
{ title: "test-collection1" }, | ||
adminHeaders | ||
) | ||
).data.collection | ||
|
||
baseCollection2 = ( | ||
await api.post( | ||
"/admin/collections", | ||
{ title: "test-collection2" }, | ||
adminHeaders | ||
) | ||
).data.collection | ||
}) | ||
|
||
describe("/store/collections", () => { | ||
describe("/store/collections/:id", () => { | ||
it("gets collection", async () => { | ||
const response = await api.get( | ||
`/store/collections/${baseCollection.id}` | ||
) | ||
|
||
expect(response.data.collection).toEqual( | ||
expect.objectContaining({ | ||
id: baseCollection.id, | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
}) | ||
) | ||
}) | ||
}) | ||
|
||
describe("/store/collections", () => { | ||
it("lists collections", async () => { | ||
const response = await api.get("/store/collections") | ||
|
||
expect(response.data).toEqual({ | ||
collections: [ | ||
expect.objectContaining({ | ||
id: baseCollection2.id, | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
id: baseCollection1.id, | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
id: baseCollection.id, | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
}), | ||
], | ||
count: 3, | ||
limit: 10, | ||
offset: 0, | ||
}) | ||
}) | ||
}) | ||
}) | ||
}, | ||
}) |
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: 18 additions & 0 deletions
18
packages/medusa/src/api-v2/store/collections/[id]/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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../types/routing" | ||
import { refetchCollection } from "../helpers" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest, | ||
res: MedusaResponse | ||
) => { | ||
const collection = await refetchCollection( | ||
req.params.id, | ||
req.scope, | ||
req.remoteQueryConfig.fields | ||
) | ||
|
||
res.status(200).json({ collection }) | ||
} |
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 refetchCollection = async ( | ||
collectionId: string, | ||
scope: MedusaContainer, | ||
fields: string[] | ||
) => { | ||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) | ||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "product_collection", | ||
variables: { | ||
filters: { id: collectionId }, | ||
}, | ||
fields: fields, | ||
}) | ||
|
||
const collections = await remoteQuery(queryObject) | ||
return collections[0] | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/medusa/src/api-v2/store/collections/middlewares.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as QueryConfig from "./query-config" | ||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" | ||
import { validateAndTransformQuery } from "../../utils/validate-query" | ||
import { | ||
StoreGetCollectionParams, | ||
StoreGetCollectionsParams, | ||
} from "./validators" | ||
|
||
export const storeCollectionRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
method: ["GET"], | ||
matcher: "/store/collections", | ||
middlewares: [ | ||
validateAndTransformQuery( | ||
StoreGetCollectionsParams, | ||
QueryConfig.listTransformQueryConfig | ||
), | ||
], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/store/collections/:id", | ||
middlewares: [ | ||
validateAndTransformQuery( | ||
StoreGetCollectionParams, | ||
QueryConfig.retrieveTransformQueryConfig | ||
), | ||
], | ||
}, | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/medusa/src/api-v2/store/collections/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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const defaultStoreCollectionFields = [ | ||
"id", | ||
"title", | ||
"handle", | ||
"created_at", | ||
"updated_at", | ||
] | ||
|
||
export const retrieveTransformQueryConfig = { | ||
defaults: defaultStoreCollectionFields, | ||
isList: false, | ||
} | ||
|
||
export const listTransformQueryConfig = { | ||
...retrieveTransformQueryConfig, | ||
defaultLimit: 10, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../types/routing" | ||
|
||
import { | ||
ContainerRegistrationKeys, | ||
remoteQueryObjectFromString, | ||
} from "@medusajs/utils" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest, | ||
res: MedusaResponse | ||
) => { | ||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) | ||
|
||
const query = remoteQueryObjectFromString({ | ||
entryPoint: "product_collection", | ||
variables: { | ||
filters: req.filterableFields, | ||
...req.remoteQueryConfig.pagination, | ||
}, | ||
fields: req.remoteQueryConfig.fields, | ||
}) | ||
|
||
const { rows: collections, metadata } = await remoteQuery(query) | ||
|
||
res.json({ | ||
collections, | ||
count: metadata.count, | ||
offset: metadata.skip, | ||
limit: metadata.take, | ||
}) | ||
} |
Oops, something went wrong.