当前位置:Gxlcms > JavaScript > JS中使用接口步骤详解

JS中使用接口步骤详解

时间:2021-07-01 10:21:17 帮助过:5人阅读

这次给大家带来JS中使用接口步骤详解,JS中使用接口的注意事项有哪些,下面就是实战案例,一起来看一下。

这篇是 js-interface 的 README,虽然并不是很复杂的一个东西,如果有人看的话我就写写源码思路了 ORZ

介绍

在做一个前后分离的项目时,有些头疼 Api 之类的东西要怎么管理,在阅读 《JavaScript 设计模式》 一书时,第二章提到了在 JavaScript 中模拟接口 (interface) 的概念,以方便使用众多设计模式,因此尝试着做一个接口的模拟。由于我本职是一名后端 Java 开发,因此希望在这个模拟层可以加入 接口默认实现接口继承方法重载 等能力,虽然这些东西加上之后不可避免得会在性能上有所牺牲,但对我来说可以提升一些开发体验(我知道 TypeScript,只是想搞个轮子试试 :P)。

使用

既然初衷是为了方便管理 Api,那么就做一个关于 Api 的 demo。

创建一个接口

  1. const config = {
  2. // 接口的名字
  3. name: 'IApi',
  4. // 是否打开此接口的 debug 开关
  5. // 开发时必须打开,否则不会启动 (方法声明、方法实现等)入参的类型检查。
  6. // 打开这个的情况下,还会获得一些调试用的信息。
  7. debug: true,
  8. }
  9. let IApi = new Interface(config)

声明方法

最简单的声明方式:

  1. IApi.method({name: 'getName'})
  2. // 等价于
  3. IApi.method({name: 'getName', args: undefined})

这样就声明了 IApi 接口含有一个 getName 方法,它没有任何参数,也没有默认实现,这就要求在后面任何 IApi 的子接口或实现类必须实现该方法,否则会抛出一个异常。


如果想指定方法的参数列表:

  1. IApi.method({ name: 'getName', args: null })

注意!

argsnull 时表示该方法可以接受任意数量的任意参数,如果重载了一个方法(详细的请参阅后面关于重载的说明):

  1. // 声明一个空参方法
  2. IApi.method({ id: 0, name: 'getName', args: null })
  3. // 重载上面的方法,使其有且只有一个 'string' 类型,名为 name 的参数
  4. IApi.method({ id: 1, name: 'getName', args: [
  5. {name: 'name', type: 'string', support: val => typeof val === 'string'}
  6. ] })

注意!

在参数描述中,type 属性只是一个字符串值,它并不真的代表参数的实际类型。它跟 name 属性一样只是提供用于调试的信息,因此你可以填入任何你认为合适的、足以标记该参数一些信息的字符串值。

真正决定方法是否接受该参数的是 support 属性,当它返回 true 时会检查下一个参数直到所有参数检查完毕或某个位置的参数不被接受。

如果需要,可以在 support 中对实际入参进行特殊处理,比如转换对象、特定属性检查等等。


如果想为方法提供默认实现:

  1. IApi.method({
  2. name: 'getName',
  3. // 默认实现,不能为箭头函数!
  4. implement: function() {
  5. return "IApi"
  6. }
  7. })

回到我们的 demo,综合运用一下:

  1. // 声明两个方法,它们都没有参数,也不需要重载因此这样就可以了
  2. IApi
  3. .method({ name: 'getName' })
  4. // 项目中使用 Axios,因此这里需要一个方法来获取 axios 实例
  5. .method({ name: 'getAxios' })
  6. // 声明四个请求方式对应的方法
  7. const methods = ['get', 'post', 'put', 'delete']
  8. methods.forEach(method => {
  9. IApi.method({
  10. name: method,
  11. args: null,
  12. implement: function() {
  13. // 处理了 this 指向问题,放心用吧
  14. return this.getAxios()[method].apply(this, arguments)
  15. .then(responseHandler)
  16. .catch(errorHandler)
  17. }
  18. })
  19. })

继承接口

假定我们要创建接口 A,要继承 B、C、D、E 等接口,使用如下语句:

  1. const A = new Interface({
  2. name: 'A',
  3. debug: true
  4. }).extends([B, C, D, E])

extends 方法会将传入的接口所持有的所有方法声明(即通过 Interface.method(config) 所声明的那些方法 )拷贝至接口 A,包括那些方法声明的默认实现。

注意!

一般来说,不会在多个接口中重载同一个方法签名,但如果真的有这样的需求,可以自行设置 id 的规则,比如:

  1. const B = new Interface(...)
  2. .method({ id: 'B00', name: 'getName', args = [...] })
  3. .method({ id: 'B01', name: 'getName', args = [...] })
  4. const C = new Interface(...)
  5. .method({ id: 'C00', name: 'getName', args = [...] })

然后实现该方法时指定要实现哪一个声明:

  1. // 注意!如果一个方法被重载,则不能在 class 中声明该方法。
  2. class AImpl { ... }
  3. const AInstance = new AImpl(...)
  4. B.implement({
  5. object: AInstance,
  6. id: 'B00', // 指定要实现的方法声明
  7. name: 'getName'
  8. })

再次回到我们的 demo,综合运用一下:

  1. const IAuthenticationApi = new Interface({
  2. name: 'IAuthentication',
  3. debug: true
  4. })
  5. // 指明 IAuthenticationApi 继承自 IApi 接口
  6. .extends(IApi)
  7. IAuthenticationApi
  8. // 重载方法 login
  9. // loin (username :string, password :string)
  10. .method({
  11. id: 0,
  12. name: 'login',
  13. args: [
  14. {name: 'username', type: 'string', support: val => typeof val === 'string'},
  15. {name: 'password', type: 'string', support: val => typeof val === 'string'}
  16. ]
  17. })
  18. // login()
  19. .method({
  20. id: 1,
  21. name: 'login'
  22. })

实现接口

  1. // 编写一个实现类
  2. class AuthenticationApi {
  3. constructor(axios) { this.axios = axios }
  4. // 直接实现 getName 方法
  5. getName() { return "AuthenticationApi" }
  6. // 直接实现 getAxios 方法
  7. getAxios() { return this.axios }
  8. }
  9. // 实现重载方法
  10. IAuthenticationApi
  11. .implement({
  12. // 指定挂载实现到 AuthenticationApi 上
  13. object: AuthenticationApi,
  14. // 指定此实现是对应 id 为 0 的方法声明
  15. id: 0,
  16. name: 'login',
  17. implement: function(username, password) {
  18. console.log('带参数的 login')
  19. // 还记得我们在 IApi 接口中定义了 get 方法(包括默认实现)吗?
  20. this.get('https://www.baidu.com')
  21. return Promise.resolve('hello')
  22. }
  23. })
  24. .implement({
  25. object: AuthenticationApi,
  26. id: 1,
  27. name: 'login',
  28. implement: function () {
  29. console.log('无参数的 login')
  30. },
  31. })
  32. IAuthenticationApi.ensureImplements(AuthenticationApi)

使用接口实现类

  1. let authenticationService = new AuthenticationApi(axios)
  2. // 挂载代理函数到实例上,否则会提示
  3. // Uncaught TypeError: authenticationService.login is not a function
  4. IAuthenticationApi.ensureImplements(authenticationService)
  5. authenticationService
  6. .login('sitdownload', '1498696873')
  7. // login(string, string) 会返回一个 Promise 还记得吗 :P
  8. .then(str => alert(`${str} world!`))
  9. authenticationService.login()

关于日志

首先确保在创建接口时打开了 debug 开关({ debug: true })。

上面的 demo 运行正常的话你将会得到下面的日志:

  1. // 注册方法
  2. Interface 注册方法: IApi.getName()
  3. Interface 注册方法: IApi.getAxios()
  4. Interface 注册方法: IApi.get(any)
  5. Interface 注册方法: IApi.post(any)
  6. Interface 注册方法: IApi.put(any)
  7. Interface 注册方法: IApi.delete(any)
  8. Interface 注册方法: IAuthentication extends IApi.getName()
  9. Interface 注册方法: IAuthentication extends IApi.getAxios()
  10. Interface 注册方法: IAuthentication extends IApi.get(any)
  11. Interface 注册方法: IAuthentication extends IApi.post(any)
  12. Interface 注册方法: IAuthentication extends IApi.put(any)
  13. Interface 注册方法: IAuthentication extends IApi.delete(any)
  14. Interface 注册方法: [0]IAuthentication.login(username :string, password :string)
  15. Interface 注册方法: [1]IAuthentication.login()
  16. // 实现方法
  17. Interface 实现方法: 保存 [0]IAuthentication.login(...) 实现:
  18. ƒ implement(username, password)
  19. Interface 实现方法: 保存 [1]IAuthentication.login(...) 实现:
  20. ƒ implement()
  21. // 匹配方法
  22. Interface 方法匹配: 精准匹配
  23. IAuthentication.login({ username: "sitdownload" } :string, { password: "1498696873" } :string).
  24. // 在控制台这行是可以打开实现的具体位置的
  25. ƒ implement(username, password)
  26. // 方法
输出 AuthenticationApi.js?7b55:25 带参数的 login // 匹配方法 Interface 方法匹配: 无法精准匹配 IAuthentication.get("https://www.baidu.com"),使用 any 实现匹配: ƒ implement() Interface 方法匹配: 精准匹配 IAuthentication.login(). ƒ implement() // 方法输出 AuthenticationApi.js?7b55:35 无参数的 login // AuthenticationApi.login(username, password) 中请求了 'https://www.baidu.com' Failed to load https://www.baidu.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access. // IApi.get(any) 中将异常直接向下抛了 Uncaught (in promise) {type: "network", payload: Error: Network Error at createError (webpack-internal:///./node_modules/_axios@0.18.0@axios/lib/…}

后续

如果要发版了,确认所有的接口方法都正确实现后,就可以把 debug 关掉,这样就不会有 Interface 内部的一些入参检查和调试输出。

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

前端面试真题解析

React结合TypeScript和Mobx步骤详解

以上就是JS中使用接口步骤详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行