提交 0a12503f 编写于 作者: 雪洛's avatar 雪洛

feat(harmony): build ext api with ets format

上级 c10eaad2
......@@ -22,32 +22,25 @@
"__APP_VIEW__": "false",
"__NODE_JS__": "false"
},
"banner": "import { IUniError, UTSObject, UTSJSONObject, string } from './uts'\nimport { defineAsyncApi } from './uni.api'",
"banner": "import { IUniError, UTSObject, string } from './uts'\nimport { UniError, UTSJSONObject } from './uni-app-harmony-framework-dev.js'\nimport { defineAsyncApi, ApiExcutor, ProtocolOptions, ApiOptions, ErrRes } from './uni-api-shared'\n",
"wrapper": {
"name": "initUniExtApi",
"args": [["APP_ID", "string"]]
}
}, {
"input": {
"../uni-runtime/src/helpers/api/index.ts": "uni-api-shared.ets"
},
"alias": {
"@dcloudio/uni-runtime": "./uni-app-harmony-framework-dev.js"
},
"externals": ["./uni-app-harmony-framework-dev.js"],
"replacements": {
"__PLATFORM__": "'app-harmony'",
"__DEV__": "false",
"__X__": "true",
"__APP_VIEW__": "false",
"__NODE_JS__": "false"
}
}
// {
// "input": {
// "src/service/index.ts": "uni.runtime.ts"
// },
// "alias": {
// "@vue/shared": "<rootDir>/packages/uni-app-harmony/src/shared",
// "@dcloudio/uni-api": "<rootDir>/packages/uni-api/src",
// "@dcloudio/uni-app-plus": "<rootDir>/packages/uni-app-plus/src",
// "@dcloudio/uni-components": "<rootDir>/packages/uni-components/src",
// "@dcloudio/uni-core": "<rootDir>/packages/uni-core/src",
// "@dcloudio/uni-i18n": "<rootDir>/packages/uni-i18n/src",
// "@dcloudio/uni-platform": "<rootDir>/packages/uni-app-harmony/src/platform",
// "@dcloudio/uni-shared": "<rootDir>/packages/uni-shared/src"
// },
// "replacements": {
// "__PLATFORM__": "'app-harmony'",
// "__DEV__": "false",
// "__X__": "true",
// "__APP_VIEW__": "false",
// "__NODE_JS__": "false"
// }
// }
]
export * from './media/chooseImage'
export * from './media/chooseVideo'
export * from './media/getImageInfo'
export * from './media/getVideoInfo'
export * from './network/request'
export * from './network/uploadFile'
export * from './network/downloadFile'
// export * from './media/chooseImage'
// export * from './media/chooseVideo'
// export * from './media/getImageInfo'
// export * from './media/getVideoInfo'
// export * from './network/request'
// export * from './network/uploadFile'
// export * from './network/downloadFile'
export * from './ui/index'
export * from './device/index'
export { navigateTo } from './route/navigateTo'
......
......@@ -178,7 +178,7 @@ export default defineConfig({
function parseExtApiInjects(uniModulesDir: string) {
return parseInjects(
true,
'app',
'app-harmony',
'arkts', // javascript|kotlin|swift (不传入)
'',
uniModulesDir,
......@@ -214,20 +214,26 @@ function initArkTSExtApi() {
if (Object.keys(injects).length === 0) {
continue
}
const specifiers: string[] = []
const apiSpecifiers: string[] = []
const apiTypeSpecifiers: string[] = []
Object.keys(injects).forEach((key) => {
const api = injects[key][1]
const apiType = capitalize(api)
specifiers.push(api)
specifiers.push(apiType)
apiSpecifiers.push(api)
apiTypeSpecifiers.push(apiType)
defineExtApis.push(api)
uniExtApis.push(`${api}: ${apiType}`)
})
importExtApis.push(
`import { ${specifiers.join(
`import { ${apiSpecifiers.join(
', '
)} } from './${extApi}/utssdk/app-harmony/index.uts'`
)
importExtApis.push(
`import { ${apiTypeSpecifiers.join(
', '
)} } from './${extApi}/utssdk/interface.uts'`
)
exportExtApis.push(
`export * from './${extApi}/utssdk/app-harmony/index.uts'`
)
......
......@@ -189,14 +189,16 @@ export function parseInjects(
fs.existsSync(
path.resolve(uniModuleRootDir, 'utssdk', 'app-android')
) ||
fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk', 'app-ios'))
fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk', 'app-ios')) ||
fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk', 'app-harmony'))
}
}
// 其他平台修改source,直接指向目标文件,否则 uts2js 找不到类型信息
if (
platform !== 'app' &&
platform !== 'app-android' &&
platform !== 'app-ios'
platform !== 'app-ios' &&
platform !== 'app-harmony'
) {
if (fs.existsSync(platformIndexFileName)) {
source = `${source}/utssdk/${platform}/index.uts`
......@@ -264,7 +266,9 @@ function parseInject(
} else {
const defineOptions = define[d] as DefineOptions
const p =
platform === 'app-android' || platform === 'app-ios'
platform === 'app-android' ||
platform === 'app-ios' ||
platform === 'app-harmony'
? 'app'
: platform
if (!(p in defineOptions)) {
......
import {
defineAsyncApi as originalDefineAsyncApi,
// defineOffApi as originalDefineOffApi,
// defineOnApi as originalDefineOnApi,
// defineSyncApi as originalDefineSyncApi,
// defineTaskApi as originalDefineTaskApi,
} from '@dcloudio/uni-runtime'
type Anything = Object | null | undefined
type NullType = null | undefined
export interface ErrRes {
errMsg?: string | null
errCode?: number | null
}
export interface ApiExcutor<K> {
resolve: (res: K | void) => void
reject: (errMsg?: string, errRes?: ErrRes) => void
}
export interface ProtocolOptions {
name?: string | null
type?: string | null
required?: boolean | null
validator?: (value: Object) => boolean | undefined | string
}
export interface ApiOptions<T> {
beforeInvoke?: (args: Object) => boolean | void | string
beforeAll?: (res: Object) => void
beforeSuccess?: (res: Object, args: T) => void
formatArgs?: Map<string, Function>
}
interface AsyncMethodOptionLike {
success?: Function | null
}
const TYPE_MAP = new Map<string, Object>([
['string', String],
['number', Number],
['boolean', Boolean],
['array', Array],
['object', Object],
])
function getPropType(type: string | NullType): Anything {
if (!type) {
return
}
return TYPE_MAP[type]
}
export function defineAsyncApi<T extends AsyncMethodOptionLike, K>(
name: string,
fn: (options: T, res: ApiExcutor<K>) => void,
protocol: Map<string, ProtocolOptions>,
options: ApiOptions<T>
): Function {
const originalProtocol = {} as Record<string, Object>
protocol.forEach((value, key) => {
const protocol = (originalProtocol[key] = {} as Record<string, Anything>)
protocol.name = value.name
protocol.type = getPropType(value.type)
protocol.required = value.required
protocol.validator = value.validator
})
const originalFormatArgs = {} as Record<string, Function>
if (options.formatArgs) {
options.formatArgs.forEach((value, key) => {
originalFormatArgs[key] = value
})
}
const originalOptions = {} as Record<string, Anything>
originalOptions.beforeInvoke = options.beforeInvoke
originalOptions.beforeAll = options.beforeAll
originalOptions.beforeSuccess = options.beforeSuccess
originalOptions.formatArgs = originalFormatArgs
return originalDefineAsyncApi<(options: T) => Promise<K>>(
name,
// @ts-expect-error
fn,
originalProtocol,
originalOptions
)
}
......@@ -6,7 +6,13 @@ export {
extend,
isArray,
} from '@vue/shared'
// export * from '@dcloudio/uni-api'
// export * from '@dcloudio/uni-shared'
// export * from '@dcloudio/uni-platform'
// export * from './helpers/api'
export { defineAsyncApi } from '@dcloudio/uni-api'
export { Emitter } from '@dcloudio/uni-shared'
export { getRealPath } from '@dcloudio/uni-platform'
export const __uniConfig = globalThis.__uniConfig
// @ts-expect-error TODO 处理类型冲突
export const UniError = globalThis.UniError
// @ts-expect-error TODO 处理类型冲突
export const UTSJSONObject = globalThis.UTSJSONObject
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册