mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.3 KiB
204 lines
5.3 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import HTTPSnippet from 'httpsnippet'
|
||
2 years ago
|
import { useClipboard } from '@vueuse/core'
|
||
2 years ago
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
2 years ago
|
import { ActiveViewInj, MetaInj } from '~/context'
|
||
2 years ago
|
import { inject, useGlobal, useProject, useSmartsheetStoreOrThrow, useVModel, useViewData } from '#imports'
|
||
2 years ago
|
|
||
|
const props = defineProps<Props>()
|
||
|
|
||
|
const emits = defineEmits(['update:modelValue'])
|
||
|
|
||
2 years ago
|
interface Props {
|
||
|
modelValue: boolean
|
||
|
}
|
||
|
|
||
2 years ago
|
const { t } = useI18n()
|
||
|
|
||
2 years ago
|
const { project } = $(useProject())
|
||
2 years ago
|
|
||
2 years ago
|
const { appInfo, token } = $(useGlobal())
|
||
2 years ago
|
|
||
|
const meta = $(inject(MetaInj)!)
|
||
|
|
||
|
const view = $(inject(ActiveViewInj)!)
|
||
|
|
||
2 years ago
|
const { xWhere } = useSmartsheetStoreOrThrow()
|
||
2 years ago
|
|
||
|
const { queryParams } = $(useViewData($$(meta), view as any, xWhere))
|
||
|
|
||
2 years ago
|
const { copy } = useClipboard()
|
||
2 years ago
|
|
||
|
let vModel = $(useVModel(props, 'modelValue', emits))
|
||
|
|
||
|
const langs = [
|
||
|
{
|
||
2 years ago
|
name: 'shell',
|
||
2 years ago
|
clients: ['curl', 'wget'],
|
||
|
},
|
||
|
{
|
||
2 years ago
|
name: 'javascript',
|
||
2 years ago
|
clients: ['axios', 'fetch', 'jquery', 'xhr'],
|
||
|
},
|
||
|
{
|
||
2 years ago
|
name: 'node',
|
||
2 years ago
|
clients: ['axios', 'fetch', 'request', 'native', 'unirest'],
|
||
|
},
|
||
|
{
|
||
2 years ago
|
name: 'nocodb-sdk',
|
||
2 years ago
|
clients: ['javascript', 'node'],
|
||
|
},
|
||
|
{
|
||
2 years ago
|
name: 'php',
|
||
2 years ago
|
},
|
||
|
{
|
||
2 years ago
|
name: 'python',
|
||
2 years ago
|
clients: ['python3', 'requests'],
|
||
|
},
|
||
|
{
|
||
2 years ago
|
name: 'ruby',
|
||
2 years ago
|
},
|
||
|
{
|
||
2 years ago
|
name: 'java',
|
||
2 years ago
|
},
|
||
|
{
|
||
2 years ago
|
name: 'c',
|
||
2 years ago
|
},
|
||
|
]
|
||
|
|
||
2 years ago
|
let selectedClient = $ref<string | undefined>(langs[0].clients && langs[0].clients[0])
|
||
2 years ago
|
|
||
2 years ago
|
const selectedLangName = $ref(langs[0].name)
|
||
2 years ago
|
|
||
2 years ago
|
const apiUrl = $computed(
|
||
|
() =>
|
||
|
new URL(`/api/v1/db/data/noco/${project.id}/${meta.title}/views/${view.title}`, (appInfo && appInfo.ncSiteUrl) || '/').href,
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
const snippet = $computed(
|
||
|
() =>
|
||
|
new HTTPSnippet({
|
||
|
method: 'GET',
|
||
|
headers: [{ name: 'xc-auth', value: token, comment: 'JWT Auth token' }],
|
||
|
url: apiUrl,
|
||
|
queryString: Object.entries(queryParams || {}).map(([name, value]) => {
|
||
|
return {
|
||
|
name,
|
||
|
value: String(value),
|
||
|
}
|
||
|
}),
|
||
|
}),
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
const activeLang = $computed(() => langs.find((lang) => lang.name === selectedLangName))
|
||
2 years ago
|
|
||
|
const code = $computed(() => {
|
||
2 years ago
|
if (activeLang?.name === 'nocodb-sdk') {
|
||
|
return `${selectedClient === 'node' ? 'const { Api } require("nocodb-sdk");' : 'import { Api } from "nocodb-sdk";'}
|
||
2 years ago
|
const api = new Api({
|
||
|
baseURL: ${JSON.stringify(apiUrl)},
|
||
|
headers: {
|
||
2 years ago
|
"xc-auth": ${JSON.stringify(token as string)}
|
||
2 years ago
|
}
|
||
|
})
|
||
|
|
||
|
api.dbViewRow.list(
|
||
|
"noco",
|
||
|
${JSON.stringify(project.title)},
|
||
2 years ago
|
${JSON.stringify(meta.title)},
|
||
|
${JSON.stringify(view.title)}, ${JSON.stringify(queryParams, null, 4)}).then(function (data) {
|
||
2 years ago
|
console.log(data);
|
||
|
}).catch(function (error) {
|
||
|
console.error(error);
|
||
|
});
|
||
|
`
|
||
|
}
|
||
|
|
||
2 years ago
|
return snippet.convert(activeLang?.name, selectedClient || (activeLang?.clients && activeLang?.clients[0]), {})
|
||
2 years ago
|
})
|
||
|
|
||
2 years ago
|
const onCopyToClipboard = () => {
|
||
|
copy(code)
|
||
2 years ago
|
// Copied to clipboard
|
||
|
message.info(t('msg.info.copiedToClipboard'))
|
||
2 years ago
|
}
|
||
|
|
||
|
const afterVisibleChange = (visible: boolean) => {
|
||
|
vModel = visible
|
||
|
}
|
||
2 years ago
|
|
||
|
watch($$(activeLang), (newLang) => {
|
||
|
selectedClient = newLang?.clients?.[0]
|
||
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<a-drawer
|
||
|
v-model:visible="vModel"
|
||
2 years ago
|
class="h-full relative"
|
||
2 years ago
|
style="color: red"
|
||
|
placement="right"
|
||
|
size="large"
|
||
|
:closable="false"
|
||
|
@after-visible-change="afterVisibleChange"
|
||
|
>
|
||
2 years ago
|
<div class="flex flex-col w-full h-full p-4">
|
||
2 years ago
|
<!-- Code Snippet -->
|
||
|
<a-typography-title :level="4" class="pb-1">{{ $t('title.codeSnippet') }}</a-typography-title>
|
||
2 years ago
|
<a-tabs v-model:activeKey="selectedLangName" class="!h-full">
|
||
|
<a-tab-pane v-for="item in langs" :key="item.name" class="!h-full">
|
||
2 years ago
|
<template #tab>
|
||
2 years ago
|
<div class="uppercase !text-xs select-none">
|
||
2 years ago
|
{{ item.name }}
|
||
2 years ago
|
</div>
|
||
|
</template>
|
||
2 years ago
|
<monaco-editor
|
||
|
class="h-[60vh] border-1 border-gray-100 py-4 rounded-sm"
|
||
|
:model-value="code"
|
||
|
:read-only="true"
|
||
|
lang="typescript"
|
||
2 years ago
|
:validate="false"
|
||
|
:disable-deep-compare="true"
|
||
2 years ago
|
hide-minimap
|
||
2 years ago
|
/>
|
||
2 years ago
|
<div v-if="activeLang.clients" class="flex flex-row w-full justify-end space-x-3 mt-4 uppercase">
|
||
2 years ago
|
<a-select v-if="activeLang" v-model:value="selectedClient" style="width: 6rem">
|
||
|
<a-select-option v-for="(client, i) in activeLang?.clients" :key="i" class="!w-full uppercase" :value="client">
|
||
|
{{ client }}
|
||
|
</a-select-option>
|
||
|
</a-select>
|
||
2 years ago
|
<a-button
|
||
|
v-t="[
|
||
|
'c:snippet:copy',
|
||
|
{ client: activeLang?.clients && (selectedClient || activeLang?.clients[0]), lang: activeLang?.name },
|
||
|
]"
|
||
|
type="primary"
|
||
|
@click="onCopyToClipboard"
|
||
2 years ago
|
>{{ $t('general.copy') }}
|
||
2 years ago
|
</a-button>
|
||
2 years ago
|
</div>
|
||
|
|
||
|
<div class="absolute bottom-4 flex flex-row justify-center w-[95%]">
|
||
|
<a
|
||
|
v-t="['e:hiring']"
|
||
2 years ago
|
class="px-4 py-2 ! rounded shadow"
|
||
2 years ago
|
href="https://angel.co/company/nocodb"
|
||
|
target="_blank"
|
||
|
@click.stop
|
||
|
>
|
||
2 years ago
|
🚀 {{ $t('labels.weAreHiring') }}! 🚀
|
||
2 years ago
|
</a>
|
||
|
</div>
|
||
2 years ago
|
</a-tab-pane>
|
||
|
</a-tabs>
|
||
|
</div>
|
||
|
</a-drawer>
|
||
|
</template>
|
||
2 years ago
|
|
||
2 years ago
|
<style scoped>
|
||
2 years ago
|
:deep(.ant-tabs-tab + .ant-tabs-tab) {
|
||
|
@apply !ml-7;
|
||
2 years ago
|
}
|
||
|
</style>
|