@abcnews/capi-fetch 中文文档教程

发布于 3年前 浏览 26 项目主页 更新于 3年前

@abcnews/capi-fetch

根据当前执行域,从 Preview / Live Content API 抓取一个 Core Media 文档对象

$ npm i @abcnews/capi-fetch

Usage

import capiFetch from '@abcnews/capi-fetch';

capiFetch(10736062, (err, doc) => {
  if (!err) {
    console.log(doc);
    // > {executionTime: "367 ms", id: 10736062, docType: "Article", ...}
  }
});

如果您项目的 JS 当前正在 nucwed.aus.aunty.abc.net.au 上的页面中执行,将向 Preview CAPI (http://nucwed.aus.aunty.abc.net.au/api/v2/*) 发出请求,否则将向 Live CAPI (>https://content-gateway.abc-prod.net.au/api/v2/*)。

如果您想将单个请求定向到 Live CAPI,而不考虑当前的执行域,请将 true 作为第三个参数传递给 capiFetch

API

declare function capiFetch(
  cmid: string | number,
  done: (err?: ProgressEvent | Error, doc?: Object) => void,
  forceLive?: boolean,
  forcePreview?: boolean
): void;

Developing

跨域请求限制......

  1. Add this to your hosts file:
127.0.0.1 mock.www.abc.net.au
127.0.0.1 mock.nucwed.aus.aunty.abc.net.au
  1. Start the development server: $ npm start
  2. Open mock.nucwed.aus.aunty.abc.net.au:8080 or mock.www.abc.net.au:8080
  3. Restore your hosts file when you're finished

要运行 /example 项目,您需要假装您的本地计算机是 www.abc.net.au 以绕过 Content API的 检查预览/实时切换是否有效的最佳方法是在 *.abc.net.au 上托管的项目中使用此模块。

出于测试目的,您可以通过将 ?prod=1 附加到当前页面 URL 来将所有请求定向到 Live CAPI。

Authors

@abcnews/capi-fetch

Grab a Core Media document object from the Preview / Live Content API, based on the current execution domain

$ npm i @abcnews/capi-fetch

Usage

import capiFetch from '@abcnews/capi-fetch';

capiFetch(10736062, (err, doc) => {
  if (!err) {
    console.log(doc);
    // > {executionTime: "367 ms", id: 10736062, docType: "Article", ...}
  }
});

If your project's JS is currently executing in a page on nucwed.aus.aunty.abc.net.au, requests will be made to Preview CAPI (http://nucwed.aus.aunty.abc.net.au/api/v2/*), otherwise they'll be made to Live CAPI (https://content-gateway.abc-prod.net.au/api/v2/*).

If you want to direct a single request to Live CAPI, regardless of the current execution domain, pass true as a 3rd argument to capiFetch.

API

declare function capiFetch(
  cmid: string | number,
  done: (err?: ProgressEvent | Error, doc?: Object) => void,
  forceLive?: boolean,
  forcePreview?: boolean
): void;

Developing

To run the /example project, you need to pretend your local machine is www.abc.net.au to work around cross-domain request limitations on the Content APIs…

  1. Add this to your hosts file:
127.0.0.1 mock.www.abc.net.au
127.0.0.1 mock.nucwed.aus.aunty.abc.net.au
  1. Start the development server: $ npm start
  2. Open mock.nucwed.aus.aunty.abc.net.au:8080 or mock.www.abc.net.au:8080
  3. Restore your hosts file when you're finished

The best way to check that the Preview / Live switching works is to use this module inside a project hosted on *.abc.net.au.

For testing purposes, you can direct all requests to Live CAPI by appending ?prod=1 to your current page URL.

Authors

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文