Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Adds unstable_pages module to JS API #2526

Merged
merged 4 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-planets-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Adds unstable_pages module to JS API
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 34 additions & 37 deletions packages/wrangler/src/__tests__/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2543,58 +2543,55 @@ and that at least one include rule is provided.
);

await runWrangler("pages project upload .");

expect(requests.length).toBe(3);

const resolvedRequests = await Promise.all(
requests.map(async (req) => await req.json<UploadPayloadFile[]>())
);

const sortedRequests = resolvedRequests.sort((a, b) => {
const aKey = a[0].key;
const bKey = b[0].key;
const resolvedRequests = (
await Promise.all(
requests.map(async (req) => await req.json<UploadPayloadFile[]>())
)
).flat();

return aKey.localeCompare(bKey);
});
const requestMap = resolvedRequests.reduce<{
[key: string]: UploadPayloadFile;
}>(
(requestMap, req) => Object.assign(requestMap, { [req.key]: req }),
{}
);

for (const req of requests) {
expect(req.headers.get("Authorization")).toBe(
"Bearer <<funfetti-auth-jwt>>"
);
}

expect(sortedRequests[0]).toMatchObject([
{
base64: true,
key: "09a79777abda8ccc8bdd51dd3ff8e9e9",
metadata: {
contentType: "application/javascript",
},
value: "ZnVuYw==",
expect(Object.keys(requestMap).length).toBe(3);

expect(requestMap["95dedb64e6d4940fc2e0f11f711cc2f4"]).toMatchObject({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GregBrimble I found it more reliable to not sort the requests and lookup by index. But rather key the requests by the key property and lookup by said key property

base64: true,
key: "95dedb64e6d4940fc2e0f11f711cc2f4",
metadata: {
contentType: "application/octet-stream",
},
]);
value: "aGVhZGVyc2ZpbGU=",
});

expect(sortedRequests[1]).toMatchObject([
{
base64: true,
key: "2082190357cfd3617ccfe04f340c6247",
metadata: {
contentType: "image/png",
},
value: "Zm9vYmFy",
expect(requestMap["2082190357cfd3617ccfe04f340c6247"]).toMatchObject({
base64: true,
key: "2082190357cfd3617ccfe04f340c6247",
metadata: {
contentType: "image/png",
},
]);
value: "Zm9vYmFy",
});

expect(sortedRequests[2]).toMatchObject([
{
base64: true,
key: "95dedb64e6d4940fc2e0f11f711cc2f4",
metadata: {
contentType: "application/octet-stream",
},
value: "aGVhZGVyc2ZpbGU=",
expect(requestMap["09a79777abda8ccc8bdd51dd3ff8e9e9"]).toMatchObject({
base64: true,
key: "09a79777abda8ccc8bdd51dd3ff8e9e9",
metadata: {
contentType: "application/javascript",
},
]);
value: "ZnVuYw==",
});

expect(std.out).toMatchInlineSnapshot(`
"✨ Success! Uploaded 3 files (TIMINGS)
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { unstable_dev } from "./dev";
export type { UnstableDevWorker, UnstableDevOptions } from "./dev";
export { unstable_pages } from "./pages";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to export the types for unstable_pages too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind, I'd like to hold off on that rabbit hole. There's a lot of types involved and I'd like to move more code around to export all of the relevant bits. If a consumer needs the type of the argument to publish or the return type, then they can easily infer it:

type PublishOptions = typeof unstable_pages.publish extends (options: infer P) => any ? P : never
type PublishResult = typeof unstable_pages.publish extends (options: any) => infer P ? P : never

I - being the only consumer of this code so far - don't actually need that yet :)

5 changes: 5 additions & 0 deletions packages/wrangler/src/api/pages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { publish } from "./publish";

export const unstable_pages = {
publish,
};