index.js 2.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  hasOwn
} from 'uni-shared'

fxy060608's avatar
fxy060608 已提交
5 6
import {
  promisify
fxy060608's avatar
fxy060608 已提交
7
} from '../helpers/promise'
fxy060608's avatar
fxy060608 已提交
8 9 10

import {
  upx2px
11
} from './upx2px'
fxy060608's avatar
fxy060608 已提交
12

fxy060608's avatar
fxy060608 已提交
13 14
import wrapper from './wrapper'

fxy060608's avatar
fxy060608 已提交
15
import todoApi from './todo'
fxy060608's avatar
fxy060608 已提交
16 17

import * as extraApi from './extra'
fxy060608's avatar
fxy060608 已提交
18 19 20

import * as api from 'uni-platform/service/api/index.js'

fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29
import {
  protocols,
  todos,
  canIUses
} from 'uni-platform/service/api/protocols'

import createApp from './wrapper/create-app'
import createPage from './wrapper/create-page'
import createComponent from './wrapper/create-component'
30 31 32 33 34 35

todos.forEach(todoApi => {
  protocols[todoApi] = false
})

canIUses.forEach(canIUseApi => {
fxy060608's avatar
fxy060608 已提交
36 37
  const apiName = protocols[canIUseApi] && protocols[canIUseApi].name ? protocols[canIUseApi].name
    : canIUseApi
38 39 40 41
  if (!__GLOBAL__.canIUse(apiName)) {
    protocols[canIUseApi] = false
  }
})
fxy060608's avatar
fxy060608 已提交
42

fxy060608's avatar
fxy060608 已提交
43 44 45 46 47 48 49 50 51 52
let uni = {}

if (typeof Proxy !== 'undefined') {
  uni = new Proxy({}, {
    get (target, name) {
      if (name === 'upx2px') {
        return upx2px
      }
      if (api[name]) {
        return promisify(name, api[name])
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60
      }
      if (__PLATFORM__ !== 'app-plus') {
        if (extraApi[name]) {
          return promisify(name, extraApi[name])
        }
        if (todoApi[name]) {
          return promisify(name, todoApi[name])
        }
fxy060608's avatar
fxy060608 已提交
61
      }
fxy060608's avatar
fxy060608 已提交
62
      if (!hasOwn(__GLOBAL__, name) && !hasOwn(protocols, name)) {
fxy060608's avatar
fxy060608 已提交
63 64
        return
      }
fxy060608's avatar
fxy060608 已提交
65
      return promisify(name, wrapper(name, __GLOBAL__[name]))
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70
    }
  })
} else {
  uni.upx2px = upx2px

fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77
  if (__PLATFORM__ !== 'app-plus') {
    Object.keys(todoApi).forEach(name => {
      uni[name] = promisify(name, todoApi[name])
    })
    Object.keys(extraApi).forEach(name => {
      uni[name] = promisify(name, todoApi[name])
    })
fxy060608's avatar
fxy060608 已提交
78
  }
fxy060608's avatar
fxy060608 已提交
79

fxy060608's avatar
fxy060608 已提交
80 81 82 83 84
  Object.keys(api).forEach(name => {
    uni[name] = promisify(name, api[name])
  })

  Object.keys(__GLOBAL__).forEach(name => {
fxy060608's avatar
fxy060608 已提交
85 86
    if (hasOwn(__GLOBAL__, name) || hasOwn(protocols, name)) {
      uni[name] = promisify(name, wrapper(name, __GLOBAL__[name]))
fxy060608's avatar
fxy060608 已提交
87 88 89
    }
  })
}
fxy060608's avatar
fxy060608 已提交
90 91 92 93

__GLOBAL__.createApp = createApp
__GLOBAL__.createPage = createPage
__GLOBAL__.createComponent = createComponent
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99

export {
  createApp,
  createPage,
  createComponent
}
100

fxy060608's avatar
fxy060608 已提交
101
export default uni