Skip to content

Commit

Permalink
Merge branch 'develop' into feat/v2-admin-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperkristensen committed May 23, 2024
2 parents b4724b8 + 66279f1 commit b95168f
Show file tree
Hide file tree
Showing 26 changed files with 86 additions and 184 deletions.
8 changes: 5 additions & 3 deletions packages/admin-next/dashboard/src/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { currencies } from "./currencies"
import { customerGroups } from "./customer-groups"
import { customers } from "./customers"
import { fulfillmentProviders } from "./fulfillment-providers"
import { fulfillments } from "./fulfillments"
import { inventoryItems } from "./inventory"
import { invites } from "./invites"
import { orders } from "./orders"
import { payments } from "./payments"
import { priceLists } from "./price-lists"
import { productTypes } from "./product-types"
import { products } from "./products"
import { promotions } from "./promotions"
import { orders } from "./orders"
import { fulfillments } from "./fulfillments"
import { reservations } from "./reservations"
import { salesChannels } from "./sales-channels"
import { shippingOptions } from "./shipping-options"
Expand All @@ -27,6 +27,8 @@ import { taxes } from "./taxes"
import { users } from "./users"
import { workflowExecutions } from "./workflow-executions"

export const backendUrl = __BACKEND_URL__ ?? "http://localhost:9000"

export const client = {
apiKeys: apiKeys,
campaigns: campaigns,
Expand Down Expand Up @@ -58,7 +60,7 @@ export const client = {
}

export const sdk = new Medusa({
baseUrl: __BACKEND_URL__ ?? "http://localhost:9000",
baseUrl: backendUrl,
auth: {
type: "session",
},
Expand Down
5 changes: 5 additions & 0 deletions packages/admin-next/dashboard/src/lib/is-fetch-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FetchError } from "@medusajs/js-sdk"

export const isFetchError = (error: any): error is FetchError => {
return error instanceof FetchError
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { zodResolver } from "@hookform/resolvers/zod"
import { ArrowPath, Trash } from "@medusajs/icons"
import { ArrowPath, Link, Trash } from "@medusajs/icons"
import { InviteDTO } from "@medusajs/types"
import {
Alert,
Button,
Container,
Heading,
Expand All @@ -21,6 +22,7 @@ import { ActionMenu } from "../../../../../components/common/action-menu"
import { useDataTable } from "../../../../../hooks/use-data-table"
import { Form } from "../../../../../components/common/form"
import { RouteFocusModal } from "../../../../../components/route-modal"
import copy from "copy-to-clipboard"
import {
useCreateInvite,
useDeleteInvite,
Expand All @@ -29,6 +31,8 @@ import {
} from "../../../../../hooks/api/invites"
import { DataTable } from "../../../../../components/table/data-table"
import { useUserInviteTableQuery } from "../../../../../hooks/table/query/use-user-invite-table-query"
import { backendUrl } from "../../../../../lib/client"
import { isFetchError } from "../../../../../lib/is-fetch-error.ts"

const InviteUserSchema = zod.object({
email: zod.string().email(),
Expand Down Expand Up @@ -74,16 +78,18 @@ export const InviteUserForm = () => {
const { mutateAsync, isPending } = useCreateInvite()

const handleSubmit = form.handleSubmit(async (values) => {
await mutateAsync(
{
email: values.email,
},
{
onSuccess: () => {
form.reset()
},
try {
await mutateAsync({ email: values.email })
form.reset()
} catch (error) {
if (isFetchError(error) && error.status === 400) {
form.setError("root", {
type: "manual",
message: error.message,
})
return
}
)
}
})

if (isError) {
Expand All @@ -106,6 +112,17 @@ export const InviteUserForm = () => {
{t("users.inviteUserHint")}
</Text>
</div>

{form.formState.errors.root && (
<Alert
variant="error"
dismissible={false}
className="text-balance"
>
{form.formState.errors.root.message}
</Alert>
)}

<div className="flex flex-col gap-y-4">
<div className="grid grid-cols-2 gap-4">
<Form.Field
Expand Down Expand Up @@ -188,6 +205,11 @@ const InviteActions = ({ invite }: { invite: InviteDTO }) => {
await resendAsync()
}

const handleCopyInviteLink = () => {
const inviteUrl = `${backendUrl}/app/invite?token=${invite.token}`
copy(inviteUrl)
}

return (
<ActionMenu
groups={[
Expand All @@ -200,6 +222,15 @@ const InviteActions = ({ invite }: { invite: InviteDTO }) => {
},
],
},
{
actions: [
{
icon: <Link />,
label: t("users.copyInviteLink"),
onClick: handleCopyInviteLink,
},
],
},
{
actions: [
{
Expand Down
1 change: 0 additions & 1 deletion packages/cli/medusa-cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node

require("dotenv").config()
require("./dist/index.js")
12 changes: 9 additions & 3 deletions packages/core/js-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ const normalizeRequest = (

const normalizeResponse = async (resp: Response, reqHeaders: Headers) => {
if (resp.status >= 300) {
const error = new FetchError(resp.statusText, resp.status)
throw error
const jsonError = await resp.json().catch(() => ({})) as { message?: string }
throw new FetchError(
jsonError.message ?? resp.statusText,
resp.statusText,
resp.status
)
}

// If we requested JSON, we try to parse the response. Otherwise, we return the raw response.
Expand All @@ -56,9 +60,11 @@ const normalizeResponse = async (resp: Response, reqHeaders: Headers) => {

export class FetchError extends Error {
status: number | undefined
statusText: string | undefined

constructor(message: string, status?: number) {
constructor(message: string, statusText?: string, status?: number) {
super(message)
this.statusText = statusText
this.status = status
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/js-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ class Medusa {
}

export default Medusa
export { FetchError } from "./client"
12 changes: 0 additions & 12 deletions packages/core/orchestration/src/joiner/remote-joiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,6 @@ export class RemoteJoiner {
{ fieldAlias, relationships },
] of expandedRelationships) {
if (!this.serviceConfigCache.has(serviceName)) {
// If true, the relationship is an internal service from the medusa core
// If modules are being used ouside of the core, we should not be throwing
// errors when the core services are not found in cache.
// TODO: Remove when there are no more "internal" services
const isInternalServicePresent = relationships.some(
(rel) => rel.isInternalService === true
)

if (isInternalServicePresent) {
continue
}

throw new Error(`Service "${serviceName}" was not found`)
}

Expand Down
5 changes: 0 additions & 5 deletions packages/core/types/src/joiner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ export type JoinerRelationship = {
foreignKey: string
primaryKey: string
serviceName: string
/**
* If true, the relationship is an internal service from the medusa core
* TODO: Remove when there are no more "internal" services
*/
isInternalService?: boolean
/**
* In an inverted relationship the foreign key is on the other service and the primary key is on the current service
*/
Expand Down
4 changes: 0 additions & 4 deletions packages/core/types/src/modules-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ export type ModuleJoinerConfig = Omit<
}

export declare type ModuleJoinerRelationship = JoinerRelationship & {
/**
* If true, the relationship is an internal service from the medusa core TODO: Remove when there are no more "internal" services
*/
isInternalService?: boolean
/**
* If true, the link joiner will cascade deleting the relationship
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/utils/src/dal/mikro-orm/db-error-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const getConstraintInfo = (err: any) => {
return null
}

const [keys, values] = detail.match(/\([^\(.]*\)/g) || []
const [keys, values] = detail.match(/\([^\(]*\)/g) || []

if (!keys || !values) {
return null
Expand Down
18 changes: 10 additions & 8 deletions packages/core/workflows-sdk/src/helper/workflow-export.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MedusaModule } from "@medusajs/modules-sdk"
import {
LocalWorkflow,
TransactionHandlerType,
TransactionState,
} from "@medusajs/orchestration"
import { LoadedModule, MedusaContainer } from "@medusajs/types"

import { MedusaModule } from "@medusajs/modules-sdk"
import { MedusaContextType } from "@medusajs/utils"
import { MedusaContextType, isPresent } from "@medusajs/utils"
import { EOL } from "os"
import { ulid } from "ulid"
import { MedusaWorkflow } from "../medusa-workflow"
Expand Down Expand Up @@ -62,13 +61,16 @@ function createContextualWorkflowRunner<
},
...args
) => {
if (!executionContainer && !flow.container) {
executionContainer = MedusaModule.getLoadedModules().map(
(mod) => Object.values(mod)[0]
)
if (!executionContainer) {
const container_ = flow.container as MedusaContainer
if (!container_ || !isPresent(container_?.registrations)) {
executionContainer = MedusaModule.getLoadedModules().map(
(mod) => Object.values(mod)[0]
)
}
}

if (!flow.container) {
if (executionContainer) {
flow.container = executionContainer
}

Expand Down
3 changes: 0 additions & 3 deletions packages/medusa/cli.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/medusa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.20.5",
"description": "Building blocks for digital commerce",
"main": "dist/index.js",
"bin": "./cli.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
Expand Down
8 changes: 7 additions & 1 deletion packages/medusa/src/api/admin/invites/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ export const POST = async (
input: {
invites: [req.validatedBody],
},
throwOnError: false,
}

const { result, errors } = await workflow.run(input)

if (errors?.length) {
throw errors[0].error
}

const { result } = await workflow.run(input)
const invite = await refetchInvite(
result[0].id,
req.scope,
Expand Down
3 changes: 0 additions & 3 deletions packages/medusa/src/bin/medusa.js

This file was deleted.

8 changes: 4 additions & 4 deletions packages/medusa/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export default async function ({ port, directory }) {
}

const cliPath = path.resolve(
require.resolve("@medusajs/medusa"),
"../",
"bin",
"medusa.js"
require.resolve("@medusajs/medusa-cli"),
"..",
"..",
"cli.js"
)
let child = fork(cliPath, [`start`, ...args], {
execArgv: argv,
Expand Down
5 changes: 0 additions & 5 deletions packages/medusa/src/loaders/medusa-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from "@medusajs/utils"

import { asValue } from "awilix"
import { remoteQueryFetchData } from "../utils/remote-query-fetch-data"

export function mergeDefaultModules(
modulesConfig: CommonTypes.ConfigModule["modules"]
Expand Down Expand Up @@ -93,15 +92,13 @@ async function runMedusaAppMigrations({
if (revert) {
await MedusaAppMigrateDown({
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
})
} else {
await MedusaAppMigrateUp({
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
Expand Down Expand Up @@ -179,7 +176,6 @@ export const loadMedusaApp = async (
const medusaApp = await MedusaApp({
workerMode: configModule.projectConfig.worker_mode,
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
Expand Down Expand Up @@ -254,7 +250,6 @@ export async function runModulesLoader({

await MedusaApp({
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
Expand Down
1 change: 0 additions & 1 deletion packages/medusa/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from "./clean-response-data"
export * from "./exception-formatter"
export * from "./middlewares"
export * from "./omit-deep"
export * from "./remote-query-fetch-data"
export * from "./remove-undefined-properties"
Loading

0 comments on commit b95168f

Please sign in to comment.