wrangler 0.0.0-ece06ea → 0.0.0-edc4b53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/package.json +24 -15
  2. package/pages/functions/buildWorker.ts +62 -0
  3. package/pages/functions/filepath-routing.test.ts +39 -0
  4. package/pages/functions/filepath-routing.ts +221 -0
  5. package/pages/functions/identifiers.ts +78 -0
  6. package/pages/functions/routes.ts +158 -0
  7. package/pages/functions/template-worker.ts +144 -0
  8. package/src/__tests__/dev.test.tsx +66 -0
  9. package/src/__tests__/index.test.ts +254 -120
  10. package/src/__tests__/jest.setup.ts +22 -0
  11. package/src/__tests__/kv.test.ts +1106 -0
  12. package/src/__tests__/mock-cfetch.ts +171 -0
  13. package/src/__tests__/mock-dialogs.ts +65 -0
  14. package/src/__tests__/run-in-tmp.ts +19 -0
  15. package/src/__tests__/run-wrangler.ts +32 -0
  16. package/src/api/form_data.ts +71 -98
  17. package/src/api/preview.ts +29 -24
  18. package/src/api/worker.ts +38 -44
  19. package/src/cfetch/index.ts +102 -0
  20. package/src/cfetch/internal.ts +65 -0
  21. package/src/cli.ts +6 -7
  22. package/src/config.ts +476 -113
  23. package/src/dev.tsx +291 -256
  24. package/src/dialogs.tsx +4 -12
  25. package/src/index.tsx +381 -252
  26. package/src/inspect.ts +524 -0
  27. package/src/kv.tsx +110 -45
  28. package/src/module-collection.ts +64 -0
  29. package/src/pages.tsx +900 -213
  30. package/src/proxy.ts +294 -0
  31. package/src/publish.ts +67 -63
  32. package/src/sites.tsx +10 -7
  33. package/src/tail.tsx +5 -3
  34. package/src/user.tsx +31 -33
  35. package/wrangler-dist/cli.js +43444 -56347
  36. package/wrangler-dist/cli.js.map +3 -3
  37. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  38. package/src/__tests__/mock-cfetch.js +0 -46
  39. package/src/api/inspect.ts +0 -441
  40. package/src/cfetch.ts +0 -72
  41. package/src/util/fetch.ts +0 -74
package/src/user.tsx CHANGED
@@ -206,7 +206,7 @@
206
206
  */
207
207
 
208
208
  import React from "react";
209
- import { render } from "ink";
209
+ import { render, Text } from "ink";
210
210
  import Table from "ink-table";
211
211
  import SelectInput from "ink-select-input";
212
212
  import fetch from "node-fetch";
@@ -298,9 +298,9 @@ let initialised = false;
298
298
 
299
299
  // we do this because we have some async stuff
300
300
  // TODO: this should just happen in the top level
301
- // abd we should fiure out how to do top level await
301
+ // and we should figure out how to do top level await
302
302
  export async function initialise(): Promise<void> {
303
- // get refreshtoken/accesstoken from fs if exists
303
+ // get refreshToken/accessToken from fs if exists
304
304
  try {
305
305
  // if CF_API_TOKEN available, use that
306
306
  if (process.env.CF_API_TOKEN) {
@@ -350,7 +350,9 @@ export function getAPIToken(): string {
350
350
  }
351
351
 
352
352
  throwIfNotInitialised();
353
- return LocalState.accessToken?.value;
353
+ // `throwIfNotInitialised()` ensures that the accessToken is guaranteed to be defined.
354
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
355
+ return LocalState.accessToken!.value;
354
356
  }
355
357
 
356
358
  interface AccessContext {
@@ -578,19 +580,16 @@ export async function getAuthURL(scopes?: string[]): Promise<string> {
578
580
  * Refresh an access token from the remote service.
579
581
  */
580
582
  async function exchangeRefreshTokenForAccessToken(): Promise<AccessContext> {
581
- const { refreshToken } = LocalState;
582
-
583
- if (!refreshToken) {
583
+ if (!LocalState.refreshToken) {
584
584
  console.warn("No refresh token is present.");
585
585
  }
586
586
 
587
- const url = TOKEN_URL;
588
587
  const body =
589
588
  `grant_type=refresh_token&` +
590
- `refresh_token=${refreshToken?.value}&` +
589
+ `refresh_token=${LocalState.refreshToken?.value}&` +
591
590
  `client_id=${CLIENT_ID}`;
592
591
 
593
- const response = await fetch(url, {
592
+ const response = await fetch(TOKEN_URL, {
594
593
  method: "POST",
595
594
  body,
596
595
  headers: {
@@ -617,10 +616,9 @@ async function exchangeRefreshTokenForAccessToken(): Promise<AccessContext> {
617
616
  LocalState.accessToken = accessToken;
618
617
 
619
618
  if (refresh_token) {
620
- const refreshToken: RefreshToken = {
619
+ LocalState.refreshToken = {
621
620
  value: refresh_token,
622
621
  };
623
- LocalState.refreshToken = refreshToken;
624
622
  }
625
623
 
626
624
  if (scope) {
@@ -666,7 +664,6 @@ async function exchangeAuthCodeForAccessToken(): Promise<AccessContext> {
666
664
  console.warn("No authorization grant code is being passed.");
667
665
  }
668
666
 
669
- const url = TOKEN_URL;
670
667
  const body =
671
668
  `grant_type=authorization_code&` +
672
669
  `code=${encodeURIComponent(authorizationCode || "")}&` +
@@ -674,7 +671,7 @@ async function exchangeAuthCodeForAccessToken(): Promise<AccessContext> {
674
671
  `client_id=${encodeURIComponent(CLIENT_ID)}&` +
675
672
  `code_verifier=${codeVerifier}`;
676
673
 
677
- const response = await fetch(url, {
674
+ const response = await fetch(TOKEN_URL, {
678
675
  method: "POST",
679
676
  body,
680
677
  headers: {
@@ -709,10 +706,9 @@ async function exchangeAuthCodeForAccessToken(): Promise<AccessContext> {
709
706
  LocalState.accessToken = accessToken;
710
707
 
711
708
  if (refresh_token) {
712
- const refreshToken: RefreshToken = {
709
+ LocalState.refreshToken = {
713
710
  value: refresh_token,
714
711
  };
715
- LocalState.refreshToken = refreshToken;
716
712
  }
717
713
 
718
714
  if (scope) {
@@ -816,7 +812,7 @@ export async function loginOrRefreshIfRequired(): Promise<boolean> {
816
812
 
817
813
  export async function login(props?: LoginProps): Promise<boolean> {
818
814
  const urlToOpen = await getAuthURL(props?.scopes);
819
- open(urlToOpen);
815
+ await open(urlToOpen);
820
816
  // TODO: log url only if on system where it's unreliable/unavailable
821
817
  // console.log(`💁 Opened ${urlToOpen}`);
822
818
  let server;
@@ -921,15 +917,14 @@ export async function refreshToken(): Promise<boolean> {
921
917
 
922
918
  export async function logout(): Promise<void> {
923
919
  throwIfNotInitialised();
924
- const { refreshToken } = LocalState;
925
- if (!refreshToken) {
920
+ if (!LocalState.refreshToken) {
926
921
  console.log("Not logged in, exiting...");
927
922
  return;
928
923
  }
929
924
  const body =
930
925
  `client_id=${encodeURIComponent(CLIENT_ID)}&` +
931
926
  `token_type_hint=refresh_token&` +
932
- `token=${encodeURIComponent(refreshToken?.value || "")}`;
927
+ `token=${encodeURIComponent(LocalState.refreshToken?.value || "")}`;
933
928
 
934
929
  const response = await fetch(REVOKE_URL, {
935
930
  method: "POST",
@@ -978,14 +973,14 @@ export async function getAccountId() {
978
973
  });
979
974
  } catch (err) {
980
975
  // probably offline
976
+ return;
981
977
  }
982
- if (!response) return;
983
- let accountId: string;
984
- // @ts-expect-error need to type this response
985
- const responseJSON: {
978
+
979
+ let accountId: string | undefined;
980
+ const responseJSON = (await response.json()) as {
986
981
  success: boolean;
987
982
  result: { id: string; account: { id: string; name: string } }[];
988
- } = await response.json();
983
+ };
989
984
 
990
985
  if (responseJSON.success === true) {
991
986
  if (responseJSON.result.length === 1) {
@@ -1017,13 +1012,16 @@ export function ChooseAccount(props: {
1017
1012
  onSelect: (item) => void;
1018
1013
  }) {
1019
1014
  return (
1020
- <SelectInput
1021
- items={props.accounts.map((item) => ({
1022
- key: item.id,
1023
- label: item.name,
1024
- value: item,
1025
- }))}
1026
- onSelect={props.onSelect}
1027
- />
1015
+ <>
1016
+ <Text bold>Select an account from below:</Text>
1017
+ <SelectInput
1018
+ items={props.accounts.map((item) => ({
1019
+ key: item.id,
1020
+ label: item.name,
1021
+ value: item,
1022
+ }))}
1023
+ onSelect={props.onSelect}
1024
+ />
1025
+ </>
1028
1026
  );
1029
1027
  }