Skip to content

Commit

Permalink
fix: use publish in triggerFirstInvocation
Browse files Browse the repository at this point in the history
  • Loading branch information
CahidArda committed Nov 12, 2024
1 parent 189c704 commit 85f0d69
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
19 changes: 13 additions & 6 deletions examples/cloudflare-workers-hono/ci.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { Client } from "@upstash/qstash"
import { Redis } from "@upstash/redis"
import { Client as WorkflowClient } from "@upstash/workflow"
import { serve } from "@upstash/workflow/nextjs"
import { describe, test, expect } from "bun:test"
import { RedisEntry } from "./src/app"
Expand Down Expand Up @@ -35,6 +36,13 @@ const tests: TestConfig[] = [
headers: {},
expectedResult: `step 1 input: '[object Object]', type: 'object', stringified input: '{"foo":"bar"}'`
},
{
testDescription: "should allow json object",
route: "ci",
payload: {"foo":"bar"},
headers: {},
expectedResult: `step 1 input: '[object Object]', type: 'object', stringified input: '{"foo":"bar"}'`
},
{
testDescription: "should allow json with one space",
route: "ci",
Expand Down Expand Up @@ -64,17 +72,16 @@ const testEndpoint = ({
}

const redis = Redis.fromEnv()
const client = new Client({
const client = new WorkflowClient({
baseUrl: process.env.QSTASH_URL,
token: process.env.QSTASH_TOKEN!
})

const secret = "secret-" + Math.floor(Math.random() * 10000).toString()

await client.publish({
await client.trigger({
url: `${process.env.DEPLOYMENT_URL}/${route}`,
body: typeof payload === "object" ? JSON.stringify(payload) : payload,
method: "POST",
body: payload,
headers: {
"secret-header": secret,
...headers
Expand All @@ -101,8 +108,8 @@ describe("cloudflare workers", () => {
token: "mock"
})

// @ts-expect-error mocking publishJSON
qstashClient.publishJSON = async () => {
// @ts-expect-error mocking publish
qstashClient.publish = async () => {
return { messageId: "msgId" }
}

Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-12/ci.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const qstashClient = new Client({
token: "mock",
})

qstashClient.publishJSON = async () => {
qstashClient.publish = async () => {
return { messageId: "msgId" }
}

Expand Down
19 changes: 13 additions & 6 deletions examples/nextjs-pages/ci.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { RedisEntry } from "@/utils/types"
import { Client } from "@upstash/qstash"
import { Client as WorkflowClient } from "@upstash/workflow"
import { Redis } from "@upstash/redis"
import { serve } from "@upstash/workflow/nextjs"
import { describe, test, expect } from "bun:test"
Expand Down Expand Up @@ -42,6 +43,13 @@ const tests: TestConfig[] = [
},
expectedResult: `step 1 input: '[object Object]', type: 'object', stringified input: '{"foo":"bar"}'`
},
{
testDescription: "should allow json object",
route: "api/ci",
payload: {"foo":"bar"},
headers: {},
expectedResult: `step 1 input: '[object Object]', type: 'object', stringified input: '{"foo":"bar"}'`
},
{
testDescription: "should allow json with one space",
route: "api/ci",
Expand Down Expand Up @@ -75,17 +83,16 @@ const testEndpoint = ({
}

const redis = Redis.fromEnv()
const client = new Client({
const client = new WorkflowClient({
baseUrl: process.env.QSTASH_URL,
token: process.env.QSTASH_TOKEN!
})

const secret = "secret-" + Math.floor(Math.random() * 10000).toString()

await client.publish({
await client.trigger({
url: `${process.env.DEPLOYMENT_URL}/${route}`,
body: typeof payload === "object" ? JSON.stringify(payload) : payload,
method: "POST",
body: payload,
headers: {
"secret-header": secret,
...headers
Expand All @@ -111,8 +118,8 @@ describe("nextjs-pages", () => {
token: "mock"
})

// @ts-expect-error mocking publishJSON
qstashClient.publishJSON = async () => {
// @ts-expect-error mocking publish
qstashClient.publish = async () => {
return { messageId: "msgId" }
}

Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs/ci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const qstashClient = new Client({
token: "mock"
})

// @ts-expect-error mocking publishJSON
qstashClient.publishJSON = async () => {
// @ts-expect-error mocking publish
qstashClient.publish = async () => {
return { messageId: "msgId" }
}

Expand Down
9 changes: 8 additions & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export const mockQStashServer = async ({
expect(request.headers.get("authorization")).toBe(`Bearer ${token}`);
// check body
if (body) {
expect(await request.json()).toEqual(body);
const requestBody = await request.text()
let parsedBody
try {
parsedBody = JSON.parse(requestBody)
} catch {
parsedBody = requestBody
}
expect(parsedBody).toEqual(body);
} else {
expect(await request.text()).toBeFalsy();
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { WorkflowLogger } from "./logger";
export type WorkflowClient = {
batchJSON: InstanceType<typeof Client>["batchJSON"];
publishJSON: InstanceType<typeof Client>["publishJSON"];
publish: InstanceType<typeof Client>["publish"];
http: InstanceType<typeof Client>["http"];
};
/**
Expand Down
8 changes: 6 additions & 2 deletions src/workflow-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ export const triggerFirstInvocation = async <TInitialPayload>(
url: workflowContext.url,
});
try {
await workflowContext.qstashClient.publishJSON({
const body =
typeof workflowContext.requestPayload === "string"
? workflowContext.requestPayload
: JSON.stringify(workflowContext.requestPayload);
await workflowContext.qstashClient.publish({
headers,
method: "POST",
body: workflowContext.requestPayload,
body,
url: workflowContext.url,
});
return ok("success");
Expand Down

0 comments on commit 85f0d69

Please sign in to comment.