enzostvs HF staff commited on
Commit
4f085f4
1 Parent(s): f480cae

rework snippet

Browse files
components/editor/main/request.tsx CHANGED
@@ -134,6 +134,7 @@ export const Request = ({
134
  <Snippet
135
  endpoint={{ ...endpoint, path: formattedEndpoint }}
136
  parameters={parameters}
 
137
  body={formattedBody}
138
  />
139
  )}
 
134
  <Snippet
135
  endpoint={{ ...endpoint, path: formattedEndpoint }}
136
  parameters={parameters}
137
+ headers={headers}
138
  body={formattedBody}
139
  />
140
  )}
components/editor/main/snippet/index.tsx CHANGED
@@ -29,11 +29,13 @@ const LANGUAGES = [
29
 
30
  export const Snippet = ({
31
  endpoint,
 
32
  parameters,
33
  body,
34
  }: {
35
  endpoint: ApiRoute;
36
  parameters?: Record<string, any>;
 
37
  body?: Options | undefined;
38
  }) => {
39
  const [_, copyToClipboard] = useCopyToClipboard();
@@ -45,12 +47,16 @@ export const Snippet = ({
45
  const needBody = ["post", "put", "patch", "delete"].includes(
46
  method.toLocaleLowerCase()
47
  );
 
 
48
  if (language === "curl") {
49
  if (needBody && body) {
50
  return (
51
- `curl -X ${method.toLocaleUpperCase()} ${path} \\` +
 
 
52
  "\n" +
53
- ` -H "Content-Type: application/json" \\` +
54
  "\n" +
55
  ` -d '${JSON.stringify(body, null, 2)}'`
56
  );
@@ -58,28 +64,29 @@ export const Snippet = ({
58
 
59
  if (parameters) {
60
  return (
61
- `curl -X ${method.toLocaleUpperCase()} ${path}?` +
62
  Object.entries(parameters)
63
  .map(([key, value]) => `${key}=${value}`)
64
  .join("&")
65
  );
66
  }
67
 
68
- return `curl -X ${method.toLocaleUpperCase()} ${path}`;
69
  }
70
 
71
  if (language === "javascript") {
72
  if (needBody && body) {
73
- return `const response = await fetch("${path}", {
74
  method: "${method.toLocaleUpperCase()}",
75
  headers: {
76
  "Content-Type": "application/json",
 
77
  },
78
  body: JSON.stringify(${JSON.stringify(body, null, 2)}),
79
  });`;
80
  }
81
  if (parameters) {
82
- return `const response = await fetch("${path}?${Object.entries(
83
  parameters
84
  )
85
  .map(([key, value]) => `${key}=${value}`)
@@ -92,15 +99,16 @@ export const Snippet = ({
92
  if (language === "python") {
93
  if (needBody && body) {
94
  return `import requests
95
- response = requests.${method.toLocaleLowerCase()}("${path}", json=${JSON.stringify(
96
- body,
97
- null,
98
- 2
99
- )})`;
 
100
  }
101
  if (parameters) {
102
  return `import requests
103
- response = requests.${method.toLocaleLowerCase()}("${path}", params={
104
  ${Object.entries(parameters)
105
  .map(([key, value]) => `${key}: ${value}`)
106
  .join(",\n ")}
 
29
 
30
  export const Snippet = ({
31
  endpoint,
32
+ headers,
33
  parameters,
34
  body,
35
  }: {
36
  endpoint: ApiRoute;
37
  parameters?: Record<string, any>;
38
+ headers?: Record<string, any>;
39
  body?: Options | undefined;
40
  }) => {
41
  const [_, copyToClipboard] = useCopyToClipboard();
 
47
  const needBody = ["post", "put", "patch", "delete"].includes(
48
  method.toLocaleLowerCase()
49
  );
50
+ const fullpath = `${process.env.NEXT_PUBLIC_APP_APIURL}${path}`;
51
+
52
  if (language === "curl") {
53
  if (needBody && body) {
54
  return (
55
+ `curl -X ${method.toLocaleUpperCase()} ${fullpath}` +
56
+ "\n" +
57
+ ` -H 'Content-Type: application/json'` +
58
  "\n" +
59
+ ` -H 'Authorization: ${headers?.Authorization}'` +
60
  "\n" +
61
  ` -d '${JSON.stringify(body, null, 2)}'`
62
  );
 
64
 
65
  if (parameters) {
66
  return (
67
+ `curl -X ${method.toLocaleUpperCase()} ${fullpath}?` +
68
  Object.entries(parameters)
69
  .map(([key, value]) => `${key}=${value}`)
70
  .join("&")
71
  );
72
  }
73
 
74
+ return `curl -X ${method.toLocaleUpperCase()} ${fullpath}`;
75
  }
76
 
77
  if (language === "javascript") {
78
  if (needBody && body) {
79
+ return `const response = await fetch("${fullpath}", {
80
  method: "${method.toLocaleUpperCase()}",
81
  headers: {
82
  "Content-Type": "application/json",
83
+ "Authorization": "${headers?.Authorization}",
84
  },
85
  body: JSON.stringify(${JSON.stringify(body, null, 2)}),
86
  });`;
87
  }
88
  if (parameters) {
89
+ return `const response = await fetch("${fullpath}?${Object.entries(
90
  parameters
91
  )
92
  .map(([key, value]) => `${key}=${value}`)
 
99
  if (language === "python") {
100
  if (needBody && body) {
101
  return `import requests
102
+ response = requests.${method.toLocaleLowerCase()}(
103
+ "${fullpath}",
104
+ headers={
105
+ Authorization: "${headers?.Authorization}",
106
+ },
107
+ json=${JSON.stringify(body, null, 2)})`;
108
  }
109
  if (parameters) {
110
  return `import requests
111
+ response = requests.${method.toLocaleLowerCase()}("${fullpath}", params={
112
  ${Object.entries(parameters)
113
  .map(([key, value]) => `${key}: ${value}`)
114
  .join(",\n ")}