README
Apple News API Client
This is a rewrite of MicNews' API wrapper for the Apple News API.
It retains the same support for creating, reading, updating, and searching articles. It also retains support for reading and listing sections and channels.
Changes
- TypeScript Source and Declarations - This rewrite was done completely in TypeScript and is compiled down to ES5 with declaration files for backwards compatibility.
- Bundle File Fetch Method - The original repository utilized an HTTP GET request to pull down any bundled files, including images and fonts. This incarnation presently reads from a local file system location. In the future this will probably be extended to employ both methods based on a flag.
Code Documentation
TBD
Install
npm install apple-news-client
Usage
TypeScript
import { AppleNewsClient } from "apple-news-client";
const client: AppleNewsClient = new AppleNewsClient({
apiId: "<API_ID>",
apiSecret: "<API_SECRET>",
});
JavaScript
const AppleNewsClient = require("apple-news-client");
const client = new AppleNewsClient({
apiId: "<API_ID>",
apiSecret: "<API_SECRET>",
});
Methods
Note: The method code examples below are displayed in TypeScript. For ES5/ES6 JavaScript just remove the type assertions.
Any of the bellow methods with promises
Same methods name with Async appended at the end: readChannel
=> readChannelAsync
TYPESCRIPT: Returns typed promise so it can be called like client.readChannel<MyInterface>(...)
- defaults to unknown if type is omitted
async function veryDescriptiveFunctionName {
// wrap into try/catch if you wish
const data = await client.readChannelAsync({ channelId: "<CHANNEL_ID>", });
// Do something with the response here.
}
async function veryDescriptiveFunctionName {
client.readChannelAsync({ channelId: "<CHANNEL_ID>", })
.then(res => {
// Do something with the response here.
}).catch(err => {
// Handle error.
});
}
readChannel
Get the details of a specified channel.
client.readChannel(
{ channelId: "<CHANNEL_ID>", },
(error: Error, response: any) => {
// Do something with the response here.
});
listSections
Get a list of sections for a specified channel.
client.listSections(
{ channelId: "<CHANNEL_ID>", },
(error: Error, response: any) => {
// Do something with the response here.
});
readSection
Get the details of a specified section.
client.readSection(
{ sectionId: "<SECTION_ID>", },
(error: Error, response: any) => {
// Do something with the response here.
});
createArticle
Create an article on the specified channel.
const channelId: string = "<CHANNEL_ID>";
const article: any = require("<PATH_TO_ARTICLE>/article.json");
const bundleFiles: any = {
"image.jpg": "<PATH_TO_ARTICLE>/image.jpg",
"image2.png": "<PATH_TO_ARTICLE>/image2.png",
};
client.createArticle(
{ channelId, article, bundleFiles, },
(error: Error, response: any) => {
// Do something with the response here.
});
readArticle
Get the details for a specified article.
client.readArticle(
{ articleId: "<ARTICLE_ID>", },
(error: Error, response: any) => {
// Do something with the response here.
});
updateArticle
Update the specified article.
const articleId: string = "<ARTICLE_ID>";
const revision: string = "2";
const article: any = require("<PATH_TO_ARTICLE>/article.json");
client.updateArticle(
{ articleId, revision, article, },
(error: Error, response: any) => {
// Do something with the response here.
});
deleteArticle
Delete the specified article.
client.deleteArticle(
{ articleId: "<PATH_TO_ARTICLE>", },
(error: Error, response: any) => {
// Do something with the response here.
});
searchArticles
Search articles in section with specified parameters.
Optional parameters:
- Strings: fromDate, sortDir, toDate, pageToken
- Integer: pageSize
client.searchArticles(
{ sectionId: "<SECTION_ID>", },
(error: Error, response: any) => {
// Do something with the response here.
});
Build From Source
Pretty simple: clone the repository, instal the dependencies then run the build script:
git clone https://github.com/robert-fairley/apple-news-client
cd apple-news-client
npm install
npm run build
You can also of course optionally just copy the contents of the src
folder into your existing TypeScript project
and import as above, but using the relative path to the index.ts
file.
Testing
The test script is set up to run unit tests and test coverage reports.
npm test
View Unit Test Reports
First run the tests, then you can view an HTML report of unit testing results:
npm run view:test-report
View Coverage Reports
First run the tests, then you can view an HTML report of unit test coverage:
npm run view:coverage-report
License
MIT
Original work: https://github.com/micnews/apple-news
Modifications © 2018 Robert Fairley