Skip to content

Commit

Permalink
Add type and api resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Sep 1, 2024
1 parent dcafcae commit f2fc0fe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/components/AutoQueryGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,28 @@ async function refresh() {
await update()
}
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent)
async function search(args:any) {
const op = apis.value.AnyQuery
if (!op) {
console.error(Errors.NoQuery)
return
}
let requestDto = createDto(op, args)
let r = await client.api(requestDto)
let complete = delaySet(x => {
api.value.response = api.value.error = undefined
apiLoading.value = x
if (!isIOS) {
api.value = r
} else {
// Fix for iOS which doesn't pick up reactive update on initial onload
nextTick(() => api.value = r)
}
})
let r = await client.api(requestDto)
complete()
// Fix for iOS which doesn't pick up reactive update on initial onload
nextTick(() => api.value = r)
let results = mapGet(r.response as any,'results') || []
if (!r.succeeded || results.label == 0) return
// Forms.fetchLookupValues(results, this.columns, () => this.refreshResults())
Expand Down
16 changes: 14 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,24 @@ export interface UiConfig {
navigate?: (url:string) => void
assetsPathResolver?: (src:string) => string
fallbackPathResolver?: (src:string) => string
apisResolver?:(type:string|null, metaTypes?:MetadataTypes|null) => AutoQueryApis|null
apiResolver?:(name:string) => MetadataOperationType|null
typeResolver?:(name:string,namespace?:string|null) => MetadataType|null
autoQueryGridDefaults?: AutoQueryGridDefaults
storage?:Storage
tableIcon?:ImageInfo
scopeWhitelist?: {[k:string]:Function}
}

export interface AutoQueryApis {
Query?: MetadataOperationType;
QueryInto?: MetadataOperationType;
Create?: MetadataOperationType;
Update?: MetadataOperationType;
Patch?: MetadataOperationType;
Delete?: MetadataOperationType;
}

export interface UploadedFile {
fileName?: string
filePath?: string
Expand Down Expand Up @@ -378,8 +390,8 @@ export interface AuthInfo {
export interface AutoQueryConvention {
name: string;
value: string;
types: string;
valueType: string;
types?: string;
valueType?: string;
}
export interface AutoQueryInfo {
maxLimit?: number;
Expand Down
43 changes: 40 additions & 3 deletions src/use/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AppMetadata, type MetadataType, type MetadataPropertyType, type MetadataOperationType, type InputInfo, type KeyValuePair, type MetadataTypes, type AutoQueryConvention, type Filter, type RefInfo, type InputProp, type AppInfo, type MetadataTypeName, MetadataApp } from "@/types"
import { type AppMetadata, type MetadataType, type MetadataPropertyType, type MetadataOperationType, type InputInfo, type KeyValuePair, type MetadataTypes, type AutoQueryConvention, type Filter, type RefInfo, type InputProp, type AppInfo, type MetadataTypeName, type AutoQueryApis, MetadataApp } from "@/types"
import { toDate, toCamelCase, chop, map, mapGet, toDateTime, leftPart, JsonServiceClient } from '@servicestack/client'
import { computed, inject } from 'vue'
import { Sole } from './config'
Expand Down Expand Up @@ -81,7 +81,7 @@ function typeName(metaType?:MetadataTypeName) {


/** Capture AutoQuery APIs */
export class Apis
export class Apis implements AutoQueryApis
{
Query?: MetadataOperationType;
QueryInto?: MetadataOperationType;
Expand Down Expand Up @@ -128,6 +128,17 @@ export class Apis

static forType(type?:string|null, metaTypes?:MetadataTypes|null) {
let apis = new Apis()
if (Sole.config.apisResolver && type) {
const aqApis = Sole.config.apisResolver(type,metaTypes)
if (aqApis) {
apis.Query = aqApis.Query
apis.QueryInto = aqApis.QueryInto
apis.Create = aqApis.Create
apis.Update = aqApis.Update
apis.Patch = aqApis.Patch
apis.Delete = aqApis.Delete
}
}
if (type) {
metaTypes ??= Sole.metadata.value?.api
metaTypes?.operations.forEach(op => {
Expand Down Expand Up @@ -406,6 +417,10 @@ async function loadMetadata(args:{
* @param [namespace] - Find MetadataType by name and namespace
*/
export function typeOf(name?:string|null, namespace?:string|null) {
if (Sole.config.typeResolver) {
let type = Sole.config.typeResolver(name!,namespace)
if (type) return type
}
let api = Sole.metadata.value?.api
if (!api || !name) return null
let type = api.types.find(x => x.name.toLowerCase() === name.toLowerCase() && (!namespace || x.namespace == namespace))
Expand All @@ -419,6 +434,10 @@ export function typeOf(name?:string|null, namespace?:string|null) {

/** Resolve Request DTO {MetadataOperationType} by name */
export function apiOf(name:string) {
if (Sole.config.apiResolver) {
const op = Sole.config.apiResolver(name)
if (op) return op
}
let api = Sole.metadata.value?.api
if (!api) return null
let requestOp = api.operations.find(x => x.request.name.toLowerCase() === name.toLowerCase())
Expand Down Expand Up @@ -669,12 +688,30 @@ export function formatFilterValue(type:string, value:string) {
: `'${value}'`
}

function nv(name:string,value:string) { return { name, value } }

const defaultViewerConventions:AutoQueryConvention[] = [
nv("=","%"),
nv("!=","%!"),
nv(">=",">%"),
nv(">","%>"),
nv("<=","%<"),
nv("<","<%"),
nv("In","%In"),
nv("Between","%Between"),
{ name: "Starts With", value: "%StartsWith", types: "string" },
{ name: "Contains", value: "%Contains", types: "string" },
{ name: "Ends With", value: "%EndsWith", types: "string" },
{ name: "Exists", value: "%IsNotNull", valueType: "none" },
{ name: "Not Exists", value: "%IsNull", valueType: "none" },
]

export function useMetadata() {

/** Reactive accessor to Ref<MetadataTypes> */
const metadataApp = computed<AppInfo|null>(() => Sole.metadata.value?.app || null)
const metadataApi = computed<MetadataTypes|null>(() => Sole.metadata.value?.api || null)
const filterDefinitions = computed<AutoQueryConvention[]>(() => Sole.metadata.value?.plugins.autoQuery.viewerConventions || [])
const filterDefinitions = computed<AutoQueryConvention[]>(() => Sole.metadata.value?.plugins?.autoQuery?.viewerConventions || defaultViewerConventions)

tryLoad()

Expand Down

0 comments on commit f2fc0fe

Please sign in to comment.