-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Api.vue
225 lines (199 loc) Β· 5.59 KB
/
Api.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<script setup lang="ts">
import HTTPSnippet from 'httpsnippet'
import { LoadingOutlined } from '@ant-design/icons-vue'
import {
ActiveViewInj,
MetaInj,
inject,
message,
ref,
storeToRefs,
useBase,
useCopy,
useGlobal,
useI18n,
useSmartsheetStoreOrThrow,
useViewData,
watch,
} from '#imports'
const { t } = useI18n()
const baseStore = useBase()
const { base } = storeToRefs(baseStore)
const { appInfo, token } = useGlobal()
const meta = inject(MetaInj, ref())
const view = inject(ActiveViewInj, ref())
const { xWhere } = useSmartsheetStoreOrThrow()
const { queryParams } = useViewData(meta, view, xWhere)
const indicator = h(LoadingOutlined, {
style: {
fontSize: '2rem',
},
spin: true,
})
const { copy } = useCopy()
const langs = [
{
name: 'shell',
clients: ['curl', 'wget'],
},
{
name: 'javascript',
clients: ['axios', 'fetch', 'jquery', 'xhr'],
},
{
name: 'node',
clients: ['axios', 'fetch', 'request', 'native', 'unirest'],
},
{
name: 'nocodb-sdk',
clients: ['javascript', 'node'],
},
{
name: 'php',
},
{
name: 'python',
clients: ['python3', 'requests'],
},
{
name: 'ruby',
},
{
name: 'java',
},
{
name: 'c',
},
]
const selectedClient = ref<string | undefined>(langs[0].clients && langs[0].clients[0])
const selectedLangName = ref(langs[0].name)
const apiUrl = computed(
() => new URL(`/api/v2/tables/${meta.value?.id}/records`, (appInfo.value && appInfo.value.ncSiteUrl) || '/').href,
)
const snippet = computed(
() =>
new HTTPSnippet({
method: 'GET',
headers: [{ name: 'xc-auth', value: token.value, comment: 'JWT Auth token' }],
url: apiUrl.value,
queryString: [
...Object.entries(queryParams.value || {}).map(([name, value]) => {
return {
name,
value: String(value),
}
}),
{ name: 'viewId', value: view.value?.id },
],
}),
)
const activeLang = computed(() => langs.find((lang) => lang.name === selectedLangName.value))
const code = computed(() => {
if (activeLang.value?.name === 'nocodb-sdk') {
return `${selectedClient.value === 'node' ? 'const { Api } = require("nocodb-sdk");' : 'import { Api } from "nocodb-sdk";'}
const api = new Api({
baseURL: "${(appInfo.value && appInfo.value.ncSiteUrl) || '/'}",
headers: {
"xc-auth": ${JSON.stringify(token.value as string)}
}
})
api.dbViewRow.list(
"noco",
${JSON.stringify(base.value?.id)},
${JSON.stringify(meta.value?.id)},
${JSON.stringify(view.value?.id)}, ${JSON.stringify(queryParams.value, null, 4)}).then(function (data) {
console.log(data);
}).catch(function (error) {
console.error(error);
});
`
}
return snippet.value.convert(
activeLang.value?.name,
selectedClient.value || (activeLang.value?.clients && activeLang.value?.clients[0]),
{},
)
})
const onCopyToClipboard = async () => {
try {
await copy(code.value)
// Copied to clipboard
message.info(t('msg.info.copiedToClipboard'))
} catch (e: any) {
message.error(e.message)
}
}
watch(activeLang, (newLang) => {
selectedClient.value = newLang?.clients?.[0]
})
</script>
<template>
<div class="flex flex-col w-full h-full px-6 mt-1">
<NcTabs v-model:activeKey="selectedLangName" class="!h-full">
<a-tab-pane v-for="item in langs" :key="item.name" class="!h-full">
<template #tab>
<div class="text-sm capitalize select-none">
{{ item.name }}
</div>
</template>
<div class="flex flex-row w-full space-x-3 my-4 items-center">
<NcSelect
v-if="activeLang?.clients"
v-model:value="selectedClient"
style="width: 10rem"
class="capitalize"
dropdown-class-name="nc-dropdown-snippet-active-lang"
>
<a-select-option v-for="(client, i) in activeLang?.clients" :key="i" class="!w-full capitalize" :value="client">
<div class="flex items-center w-full justify-between w-full gap-2">
<div class="truncate flex-1">{{ client }}</div>
<component
:is="iconMap.check"
v-if="selectedClient === client"
id="nc-selected-item-icon"
class="text-primary w-4 h-4"
/>
</div>
</a-select-option>
</NcSelect>
<NcButton
v-e="[
'c:snippet:copy',
{ client: activeLang?.clients && (selectedClient || activeLang?.clients[0]), lang: activeLang?.name },
]"
type="secondary"
size="small"
@click="onCopyToClipboard"
>
<div class="flex items-center">
<GeneralIcon icon="copy" class="mr-1" />
{{ $t('general.copy') }} code
</div>
</NcButton>
</div>
<Suspense>
<MonacoEditor
class="border-1 border-gray-200 py-4 rounded-lg"
style="height: calc(100vh - (var(--topbar-height) * 2) - 8rem)"
:model-value="code"
:read-only="true"
lang="typescript"
:validate="false"
:disable-deep-compare="true"
hide-minimap
/>
<template #fallback>
<div class="h-full w-full flex flex-col justify-center items-center mt-28">
<a-spin size="large" :indicator="indicator" />
</div>
</template>
</Suspense>
</a-tab-pane>
</NcTabs>
</div>
</template>
<style scoped>
:deep(.ant-tabs-tab + .ant-tabs-tab) {
@apply !ml-7;
}
</style>