@55degrees/lagom-fetch 中文文档教程
What is lagom-fetch
LagomFetch 是一个抽象层,它允许基于 Atlassian 的应用程序使用相同的代码在 Atlassian Connect javascript 库(云)或 Atlassian p2 库 - AJS(服务器/数据中心)中发出请求。 (希望稍后添加 Forge 支持)。
除此之外,它还允许您在使用故事书或在 Atlassian 环境之外加载页面时模拟负载加载。
Installation
只需添加: npm install @55degrees/lagom-fetch
Then
import lagomFetch from '@55degrees/lagom-fetch';
lagomFetch(method, url, data, (err, resp)=>
{
// do stuff here
})
or
lagomFetch(method, url, data).then(resp=>
{
// do stuff here
})
Where:
- method is 'POST', 'GET', 'DELETE' etc.
- url is the url that you're calling (note that for server you do not include the context path)
- data is null for GET and DELETE methods. For other objects it is javascript object to be sent as the body;
In return 你得到一个返回对象的javascript对象。 如果出现错误 - 在回调模式下,还会返回一个错误对象。
Mock data…
如果你想模拟调用(比如在 Storybook 中)。
只需导入 PayloadRegister -
import { PayloadRegister} from "@teamlagom/lagom-fetch";
然后启用它:
PayloadRegister.enable(true);
Register a single payload
只需使用方法、url、要发送的正文和响应对象调用 registerPayload 方法)。
例如:
PayloadRegister.registerPayload('POST','/my-url-1',null, "this is a post with no contents")
如果您有一个可以根据 POST 正文做出不同响应的 url,您可以将相同的 url 注册到一个 body-to-send 对象,并使用导入参数来区分请求。 例如:
PayloadRegister.registerPayload('POST','/my-url-2',{param1:"test a"}, { a:1} )
PayloadRegister.registerPayload('POST','/my-url-2',{param1:"test b"}, {a:2} )
当 lagomFetch 使用 param1:'test a' 到 /my-url-2 的正文对象发出请求时 - 响应将是 {a:1}。 如果 param1:'test b' 被发送 - 那么响应是 {a:2}
Bulk load urls
有很多你想要加载的对象? 然后创建一个 js 散列(key 是 payload 的 id 以供将来使用)。 对象的值具有以下键/值: method:模拟的方法 url:要响应的 url body:身体匹配 response:应该发回的对象。
Delay
想要模拟响应延迟? 只需调用: PayloadRegister.setRequestDelay(时间)
其中时间是以毫秒为单位的时间。
What is lagom-fetch
LagomFetch is an abstraction layer that allows an Atlassian based app to use the same code to make requests in either Atlassian Connect javascript library (Cloud) or Atlassian p2 library - AJS (Server/Data Center). (Hoping to add Forge support later).
In addition to this - it allows you to mock payload loading for when you're using storybook or are loading pages outside of the Atlassian environment.
Installation
Simply add: npm install @55degrees/lagom-fetch
Then
import lagomFetch from '@55degrees/lagom-fetch';
lagomFetch(method, url, data, (err, resp)=>
{
// do stuff here
})
or
lagomFetch(method, url, data).then(resp=>
{
// do stuff here
})
Where:
- method is 'POST', 'GET', 'DELETE' etc.
- url is the url that you're calling (note that for server you do not include the context path)
- data is null for GET and DELETE methods. For other objects it is javascript object to be sent as the body;
In return you get a javascript object of the return of the object. If there's an error - in callback mode there is an error object also returned.
Mock data…
If you want to emulate the calls (like in a Storybook).
Simply import the PayloadRegister - by
import { PayloadRegister} from "@teamlagom/lagom-fetch";
Then enable it:
PayloadRegister.enable(true);
Register a single payload
Simply call the registerPayload method with the method, the url, body-to-send and the response object).
For example:
PayloadRegister.registerPayload('POST','/my-url-1',null, "this is a post with no contents")
If you have a url that can respond differently based on the POST body, you can register the same url with a body-to-send object with the import parameters to differentate the request on. For example:
PayloadRegister.registerPayload('POST','/my-url-2',{param1:"test a"}, { a:1} )
PayloadRegister.registerPayload('POST','/my-url-2',{param1:"test b"}, {a:2} )
When lagomFetch makes a request with a body object of param1:'test a' to /my-url-2 - the response will be {a:1}. If param1:'test b' is sent - then the response is {a:2}
Bulk load urls
Got a lot of objects that you want to load? Then create a js hash (key is an id of the payload for future usage). The value of the object has the following key/val : method: The method to mock url: The url to respond to body: The body match response: The object that should be sent back.
Delay
Want to emulate a delay on the response? Simply call: PayloadRegister.setRequestDelay(time)
where time is the time in ms.