@scalar/api-client 0.4.0 → 0.5.1

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 (62) hide show
  1. package/README.md +62 -0
  2. package/dist/components/ApiClient/Request/RequestBody.vue.d.ts.map +1 -1
  3. package/dist/components/ApiClient/Request/RequestHeaders.vue.d.ts +0 -2
  4. package/dist/components/ApiClient/Request/RequestHeaders.vue.d.ts.map +1 -1
  5. package/dist/components/ApiClient/Request/RequestQuery.vue.d.ts +0 -2
  6. package/dist/components/ApiClient/Request/RequestQuery.vue.d.ts.map +1 -1
  7. package/dist/components/ApiClient/Request/RequestVariables.vue.d.ts +2 -4
  8. package/dist/components/ApiClient/Request/RequestVariables.vue.d.ts.map +1 -1
  9. package/dist/components/ApiClient/Response/ResponseBody.vue.d.ts.map +1 -1
  10. package/dist/helpers/concatenateUrlAndPath.d.ts +5 -0
  11. package/dist/helpers/concatenateUrlAndPath.d.ts.map +1 -0
  12. package/dist/helpers/index.d.ts +6 -1
  13. package/dist/helpers/index.d.ts.map +1 -1
  14. package/dist/helpers/normalizePath.d.ts +5 -0
  15. package/dist/helpers/normalizePath.d.ts.map +1 -0
  16. package/dist/helpers/normalizeRequestMethod.d.ts +5 -0
  17. package/dist/helpers/normalizeRequestMethod.d.ts.map +1 -0
  18. package/dist/helpers/normalizeUrl.d.ts +5 -0
  19. package/dist/helpers/normalizeUrl.d.ts.map +1 -0
  20. package/dist/helpers/replaceVariables.d.ts +5 -0
  21. package/dist/helpers/replaceVariables.d.ts.map +1 -0
  22. package/dist/helpers/sendRequest.d.ts +2 -2
  23. package/dist/helpers/sendRequest.d.ts.map +1 -1
  24. package/dist/index.d.ts +0 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +374 -4181
  27. package/dist/stores/apiClientRequestStore.d.ts +8 -8
  28. package/dist/types.d.ts +7 -5
  29. package/dist/types.d.ts.map +1 -1
  30. package/package.json +26 -21
  31. package/src/components/ApiClient/AddressBar.vue +1 -1
  32. package/src/components/ApiClient/Request/Request.vue +1 -1
  33. package/src/components/ApiClient/Request/RequestBody.vue +3 -2
  34. package/src/components/ApiClient/Request/RequestHeaders.vue +2 -2
  35. package/src/components/ApiClient/Request/RequestQuery.vue +2 -2
  36. package/src/components/ApiClient/Request/RequestVariables.vue +3 -3
  37. package/src/components/ApiClient/Response/ResponseBody.vue +3 -2
  38. package/src/helpers/concatenateUrlAndPath.test.ts +27 -0
  39. package/src/helpers/concatenateUrlAndPath.ts +13 -0
  40. package/src/helpers/index.ts +6 -1
  41. package/src/helpers/normalizePath.test.ts +17 -0
  42. package/src/helpers/normalizePath.ts +16 -0
  43. package/src/helpers/normalizeRequestMethod.test.ts +29 -0
  44. package/src/helpers/normalizeRequestMethod.ts +43 -0
  45. package/src/helpers/normalizeUrl.test.ts +25 -0
  46. package/src/helpers/normalizeUrl.ts +24 -0
  47. package/src/helpers/replaceVariables.test.ts +13 -0
  48. package/src/helpers/replaceVariables.ts +11 -0
  49. package/src/helpers/sendRequest.test.ts +50 -0
  50. package/src/helpers/sendRequest.ts +43 -32
  51. package/src/index.ts +0 -1
  52. package/src/types.ts +9 -5
  53. package/dist/components/CodeMirror/CodeMirror.vue.d.ts +0 -56
  54. package/dist/components/CodeMirror/CodeMirror.vue.d.ts.map +0 -1
  55. package/dist/components/CodeMirror/extensions/variables.d.ts +0 -6
  56. package/dist/components/CodeMirror/extensions/variables.d.ts.map +0 -1
  57. package/dist/components/CodeMirror/index.d.ts +0 -2
  58. package/dist/components/CodeMirror/index.d.ts.map +0 -1
  59. package/src/components/CodeMirror/CodeMirror.vue +0 -232
  60. package/src/components/CodeMirror/extensions/variables.ts +0 -41
  61. package/src/components/CodeMirror/index.ts +0 -1
  62. package/src/hooks/useOperation.test.ts +0 -7
@@ -1,20 +1,20 @@
1
1
  import axios from 'axios'
2
2
  import { nanoid } from 'nanoid'
3
- import nunjucks from 'nunjucks'
4
3
 
5
4
  import type {
6
5
  ClientRequestConfig,
7
6
  ClientResponse,
8
7
  RequestResult,
8
+ SendRequestConfig,
9
9
  } from '../types'
10
- import { mapFromArray } from './mapFromArray'
11
-
12
- const templateEngine = nunjucks.configure({
13
- tags: {
14
- variableStart: '{',
15
- variableEnd: '}',
16
- },
17
- })
10
+ import {
11
+ concatenateUrlAndPath,
12
+ mapFromArray,
13
+ normalizePath,
14
+ normalizeRequestMethod,
15
+ normalizeUrl,
16
+ replaceVariables,
17
+ } from './'
18
18
 
19
19
  const defaultHeaders = {
20
20
  'User-Agent': 'Scalar API Client',
@@ -24,24 +24,26 @@ const defaultHeaders = {
24
24
  * Send a request via the proxy
25
25
  */
26
26
  export async function sendRequest(
27
- request: ClientRequestConfig,
28
- proxyUrl: string,
27
+ request: SendRequestConfig,
28
+ proxyUrl?: string,
29
29
  ): Promise<RequestResult | null> {
30
- // Format complete URL
31
- const method = request.type.toUpperCase()
32
- const fullUrl = `${request.url}${request.path}`
30
+ const method = normalizeRequestMethod(request.type)
33
31
  const headers: Record<string, string | number> = {
34
32
  ...defaultHeaders,
35
- ...mapFromArray(request.headers, 'name', 'value'),
33
+ ...mapFromArray(request.headers ?? [], 'name', 'value'),
36
34
  }
35
+ const url = normalizeUrl(request.url)
36
+ const path = normalizePath(request.path)
37
+ const urlWithPath = concatenateUrlAndPath(url, path)
38
+ const renderedURL = replaceVariables(
39
+ urlWithPath,
40
+ mapFromArray(request.parameters ?? [], 'name', 'value'),
41
+ )
42
+
37
43
  /** TODO: Make dynamic */
38
44
  const auth = {
39
45
  type: 'none',
40
46
  }
41
- const variables = mapFromArray(request.parameters, 'name', 'value')
42
- const renderedURL = templateEngine.renderString(fullUrl, variables)
43
- /** TODO: Make dynamic */
44
- const proxy = true
45
47
 
46
48
  const startTime = Date.now()
47
49
 
@@ -53,7 +55,7 @@ export async function sendRequest(
53
55
  data: request.body,
54
56
  }
55
57
 
56
- const config = proxy
58
+ const config = proxyUrl
57
59
  ? {
58
60
  method: 'POST',
59
61
  url: proxyUrl,
@@ -71,24 +73,33 @@ export async function sendRequest(
71
73
  const response: (ClientResponse & { error: false }) | { error: true } =
72
74
  // @ts-ignore
73
75
  await axios(config)
74
- .then((res) => ({
75
- ...res.data,
76
- error: false,
77
- }))
78
- .catch((err) => ({
79
- error: true,
80
- ...err?.response,
81
- }))
76
+ .then((res) => {
77
+ return {
78
+ ...res.data,
79
+ error: false,
80
+ }
81
+ })
82
+ .catch((err) => {
83
+ return {
84
+ error: true,
85
+ ...err?.response,
86
+ }
87
+ })
82
88
 
83
- return !response.error
84
- ? {
89
+ return response.error
90
+ ? null
91
+ : {
85
92
  sentTime: Date.now(),
86
- request,
93
+ request: {
94
+ ...request,
95
+ type: method,
96
+ url,
97
+ path,
98
+ },
87
99
  response: {
88
100
  ...response,
89
101
  duration: Date.now() - startTime,
90
102
  },
91
103
  responseId: nanoid(),
92
104
  }
93
- : null
94
105
  }
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { ApiClient } from './components/ApiClient'
2
- export { CodeMirror } from './components/CodeMirror'
3
2
 
4
3
  export * from './helpers'
5
4
  export * from './hooks'
package/src/types.ts CHANGED
@@ -51,20 +51,21 @@ export type FormDataItem = BaseParameter
51
51
  /** Complete request state for a client request */
52
52
  export type ClientRequestConfig = {
53
53
  id?: string
54
- name: string
54
+ name?: string
55
55
  url: string
56
56
  /** HTTP Request Method */
57
57
  type: string
58
58
  /** Request path */
59
59
  path: string
60
+ /** TODO: Rename to variables? */
60
61
  /** Path parameters */
61
- parameters: BaseParameter[]
62
+ parameters?: BaseParameter[]
62
63
  /** Query parameters */
63
- query: Query[]
64
+ query?: Query[]
64
65
  /** Request headers */
65
- headers: Header[]
66
+ headers?: Header[]
66
67
  /** Content type matched body */
67
- body: string
68
+ body?: string
68
69
  /** Optional form data body */
69
70
  formData?: FormDataItem[]
70
71
  }
@@ -89,6 +90,9 @@ export type ClientResponse = {
89
90
  duration: number
90
91
  }
91
92
 
93
+ export type SendRequestConfig = Partial<ClientRequestConfig> &
94
+ Required<Pick<ClientRequestConfig, 'url'>>
95
+
92
96
  export type RequestResult = {
93
97
  request: ClientRequestConfig
94
98
  response: ClientResponse
@@ -1,56 +0,0 @@
1
- type Language = 'axios' | 'c' | 'clojure' | 'csharp' | 'go' | 'http' | 'java' | 'javascript' | 'json' | 'kotlin' | 'node' | 'objc' | 'ocaml' | 'powershell' | 'python' | 'r' | 'ruby' | 'shell' | 'swift' | 'php';
2
- declare const _default: import("vue").DefineComponent<{
3
- content: {
4
- type: import("vue").PropType<string>;
5
- };
6
- readOnly: {
7
- type: import("vue").PropType<boolean>;
8
- };
9
- languages: {
10
- type: import("vue").PropType<Language[]>;
11
- };
12
- withVariables: {
13
- type: import("vue").PropType<boolean>;
14
- };
15
- lineNumbers: {
16
- type: import("vue").PropType<boolean>;
17
- };
18
- withoutTheme: {
19
- type: import("vue").PropType<boolean>;
20
- };
21
- disableEnter: {
22
- type: import("vue").PropType<boolean>;
23
- default: boolean;
24
- };
25
- }, {}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
26
- change: (value: string) => void;
27
- }, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
28
- content: {
29
- type: import("vue").PropType<string>;
30
- };
31
- readOnly: {
32
- type: import("vue").PropType<boolean>;
33
- };
34
- languages: {
35
- type: import("vue").PropType<Language[]>;
36
- };
37
- withVariables: {
38
- type: import("vue").PropType<boolean>;
39
- };
40
- lineNumbers: {
41
- type: import("vue").PropType<boolean>;
42
- };
43
- withoutTheme: {
44
- type: import("vue").PropType<boolean>;
45
- };
46
- disableEnter: {
47
- type: import("vue").PropType<boolean>;
48
- default: boolean;
49
- };
50
- }>> & {
51
- onChange?: ((value: string) => any) | undefined;
52
- }, {
53
- disableEnter: boolean;
54
- }, {}>;
55
- export default _default;
56
- //# sourceMappingURL=CodeMirror.vue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CodeMirror.vue.d.ts","sourceRoot":"","sources":["../../../src/components/CodeMirror/CodeMirror.vue.ts"],"names":[],"mappings":"AA+EA,KAAK,QAAQ,GACT,OAAO,GACP,GAAG,GACH,SAAS,GACT,QAAQ,GACR,IAAI,GACJ,MAAM,GACN,MAAM,GACN,YAAY,GACZ,MAAM,GACN,QAAQ,GACR,MAAM,GACN,MAAM,GACN,OAAO,GACP,YAAY,GACZ,QAAQ,GACR,GAAG,GACH,MAAM,GACN,OAAO,GACP,OAAO,GACP,KAAK,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4JT,wBAAkD"}
@@ -1,6 +0,0 @@
1
- import { type DecorationSet, ViewPlugin, type ViewUpdate } from '@codemirror/view';
2
- export declare const variables: () => ViewPlugin<{
3
- variables: DecorationSet;
4
- update(update: ViewUpdate): void;
5
- }>;
6
- //# sourceMappingURL=variables.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"variables.d.ts","sourceRoot":"","sources":["../../../../src/components/CodeMirror/extensions/variables.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAGlB,UAAU,EACV,KAAK,UAAU,EAChB,MAAM,kBAAkB,CAAA;AAYzB,eAAO,MAAM,SAAS;;mBAOD,UAAU;EAc5B,CAAA"}
@@ -1,2 +0,0 @@
1
- export { default as CodeMirror } from './CodeMirror.vue';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/CodeMirror/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,kBAAkB,CAAA"}
@@ -1,232 +0,0 @@
1
- <script lang="ts" setup>
2
- import { java } from '@codemirror/lang-java'
3
- import { javascript } from '@codemirror/lang-javascript'
4
- import { json } from '@codemirror/lang-json'
5
- import { python } from '@codemirror/lang-python'
6
- import { type LanguageSupport, StreamLanguage } from '@codemirror/language'
7
- import {
8
- c,
9
- csharp,
10
- kotlin,
11
- objectiveC,
12
- } from '@codemirror/legacy-modes/mode/clike'
13
- import { clojure } from '@codemirror/legacy-modes/mode/clojure'
14
- import { go } from '@codemirror/legacy-modes/mode/go'
15
- import { http } from '@codemirror/legacy-modes/mode/http'
16
- import { oCaml } from '@codemirror/legacy-modes/mode/mllike'
17
- import { powerShell } from '@codemirror/legacy-modes/mode/powershell'
18
- import { r } from '@codemirror/legacy-modes/mode/r'
19
- import { ruby } from '@codemirror/legacy-modes/mode/ruby'
20
- import { shell } from '@codemirror/legacy-modes/mode/shell'
21
- import { swift } from '@codemirror/legacy-modes/mode/swift'
22
- import { type Extension } from '@codemirror/state'
23
- import {
24
- EditorView,
25
- type ViewUpdate,
26
- keymap,
27
- lineNumbers as lineNumbersExtension,
28
- } from '@codemirror/view'
29
- import { useCodeMirror } from '@scalar/use-codemirror'
30
- import { watch } from 'vue'
31
-
32
- import { variables } from './extensions/variables'
33
-
34
- const props = withDefaults(
35
- defineProps<{
36
- content?: string
37
- readOnly?: boolean
38
- languages?: Language[]
39
- withVariables?: boolean
40
- lineNumbers?: boolean
41
- withoutTheme?: boolean
42
- disableEnter?: boolean
43
- }>(),
44
- {
45
- disableEnter: false,
46
- },
47
- )
48
-
49
- const emit = defineEmits<{
50
- (e: 'change', value: string): void
51
- }>()
52
-
53
- // TODO: Add 'php' and 'laravel'
54
- const syntaxHighlighting: Partial<
55
- Record<Language, LanguageSupport | StreamLanguage<any>>
56
- > = {
57
- axios: javascript(),
58
- c: StreamLanguage.define(c),
59
- clojure: StreamLanguage.define(clojure),
60
- csharp: StreamLanguage.define(csharp),
61
- go: StreamLanguage.define(go),
62
- http: StreamLanguage.define(http),
63
- java: java(),
64
- javascript: javascript(),
65
- json: json(),
66
- kotlin: StreamLanguage.define(kotlin),
67
- node: javascript(),
68
- objc: StreamLanguage.define(objectiveC),
69
- ocaml: StreamLanguage.define(oCaml),
70
- powershell: StreamLanguage.define(powerShell),
71
- python: python(),
72
- r: StreamLanguage.define(r),
73
- ruby: StreamLanguage.define(ruby),
74
- shell: StreamLanguage.define(shell),
75
- swift: StreamLanguage.define(swift),
76
- }
77
-
78
- type Language =
79
- | 'axios'
80
- | 'c'
81
- | 'clojure'
82
- | 'csharp'
83
- | 'go'
84
- | 'http'
85
- | 'java'
86
- | 'javascript'
87
- | 'json'
88
- | 'kotlin'
89
- | 'node'
90
- | 'objc'
91
- | 'ocaml'
92
- | 'powershell'
93
- | 'python'
94
- | 'r'
95
- | 'ruby'
96
- | 'shell'
97
- | 'swift'
98
- | 'php'
99
-
100
- // CSS Class
101
- const classes = ['scalar-api-client__codemirror']
102
-
103
- if (props.readOnly) {
104
- classes.push('scalar-api-client__codemirror--read-only')
105
- }
106
-
107
- const getCodeMirrorExtensions = () => {
108
- const extensions: Extension[] = []
109
-
110
- extensions.push(EditorView.editorAttributes.of({ class: classes.join(' ') }))
111
-
112
- // Read only
113
- if (props.readOnly) {
114
- extensions.push(EditorView.editable.of(false))
115
- }
116
-
117
- // Syntax highlighting
118
- if (props.languages) {
119
- props.languages
120
- .filter((language) => typeof syntaxHighlighting[language] !== 'undefined')
121
- .forEach((language) => {
122
- extensions.push(syntaxHighlighting[language] as Extension)
123
- })
124
- }
125
-
126
- // Line numbers
127
- if (props.lineNumbers) {
128
- extensions.push(lineNumbersExtension())
129
- }
130
-
131
- // Highlight variables
132
- if (props.withVariables) {
133
- extensions.push(variables())
134
- }
135
-
136
- if (props.disableEnter) {
137
- extensions.push(
138
- keymap.of([
139
- {
140
- key: 'Enter',
141
- run: () => {
142
- return true
143
- },
144
- },
145
- {
146
- key: 'Ctrl-Enter',
147
- mac: 'Cmd-Enter',
148
- run: () => {
149
- return true
150
- },
151
- },
152
- {
153
- key: 'Shift-Enter',
154
- run: () => {
155
- return true
156
- },
157
- },
158
- ]),
159
- )
160
- }
161
-
162
- // Listen to updates
163
- extensions.push(
164
- EditorView.updateListener.of((v: ViewUpdate) => {
165
- if (!v.docChanged) {
166
- return
167
- }
168
-
169
- emit('change', v.state.doc.toString())
170
- }),
171
- )
172
-
173
- return extensions
174
- }
175
-
176
- const { codeMirrorRef, setCodeMirrorContent, reconfigureCodeMirror } =
177
- useCodeMirror({
178
- content: props.content ?? '',
179
- extensions: getCodeMirrorExtensions(),
180
- withoutTheme: props.withoutTheme,
181
- })
182
-
183
- watch(props, () => {
184
- setCodeMirrorContent(props.content ?? '')
185
- reconfigureCodeMirror(getCodeMirrorExtensions())
186
- })
187
- </script>
188
-
189
- <template>
190
- <div
191
- ref="codeMirrorRef"
192
- class="scalar-api-client__codemirror__wrapper" />
193
- </template>
194
-
195
- <style>
196
- /** Basics */
197
- .scalar-api-client__codemirror__wrapper {
198
- width: 100%;
199
- display: flex;
200
- align-items: stretch;
201
- }
202
-
203
- .scalar-api-client__codemirror {
204
- flex-grow: 1;
205
- max-width: 100%;
206
- }
207
-
208
- .scalar-api-client__codemirror.ͼw {
209
- background-color: var(--theme-background-1);
210
- }
211
-
212
- .scalar-api-client__codemirror--read-only.ͼw {
213
- background-color: var(--theme-background-2);
214
- }
215
-
216
- /** URL input */
217
- .scalar-api-client__url-input {
218
- font-weight: var(--theme-semibold);
219
- }
220
-
221
- .scalar-api-client__url-input .cm-scroller {
222
- padding-left: 6px;
223
- }
224
-
225
- .scalar-api-client__url-input .ͼ1 .cm-scroller {
226
- align-items: center !important;
227
- }
228
-
229
- .scalar-api-client__variable {
230
- color: var(--scalar-api-client-color);
231
- }
232
- </style>
@@ -1,41 +0,0 @@
1
- import {
2
- Decoration,
3
- type DecorationSet,
4
- EditorView,
5
- MatchDecorator,
6
- ViewPlugin,
7
- type ViewUpdate,
8
- } from '@codemirror/view'
9
-
10
- const variableHighlighterDecoration = new MatchDecorator({
11
- regexp: /(\{[^}]+\})/g,
12
- decoration: () =>
13
- Decoration.mark({
14
- attributes: {
15
- class: 'scalar-api-client__variable',
16
- },
17
- }),
18
- })
19
-
20
- export const variables = () =>
21
- ViewPlugin.fromClass(
22
- class {
23
- variables: DecorationSet
24
- constructor(view: EditorView) {
25
- this.variables = variableHighlighterDecoration.createDeco(view)
26
- }
27
- update(update: ViewUpdate) {
28
- this.variables = variableHighlighterDecoration.updateDeco(
29
- update,
30
- this.variables,
31
- )
32
- }
33
- },
34
- {
35
- decorations: (instance) => instance.variables,
36
- provide: (plugin) =>
37
- EditorView.atomicRanges.of(
38
- (view) => view.plugin(plugin)?.variables || Decoration.none,
39
- ),
40
- },
41
- )
@@ -1 +0,0 @@
1
- export { default as CodeMirror } from './CodeMirror.vue'
@@ -1,7 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- describe('example', () => {
4
- it('executes tests', () => {
5
- expect(true).toBe(true)
6
- })
7
- })