gpt4 book ai didi

javascript - 定义辅助链函数

转载 作者:行者123 更新时间:2023-12-03 01:33:29 25 4
gpt4 key购买 nike

我经常在授权的情况下执行 chai 请求:

chai.request(baseUrl).get(`/resource`).set('authorization', `Bearer ${token}`)

我想将授权分解到一个名为 withAuth 的函数中,这样我就可以更方便地重用它:

chai.request(baseUrl).get(`/resource`).withAuth()

但我不知道该怎么做?我尝试过:

let withAuth = () => {set('authorization', `Bearer ${token}`)}

但这行不通。

最佳答案

使用断言原型(prototype)的addChainableMethod:

chai.Assertion.addChainableMethod('withAuth',(request) => request.set('authorization', `Bearer ${token}`));

您可以引用this documentation看看utility method addChainableMethod

关于javascript - 定义辅助链函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com