Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updated getSubstackFeedByLink method signature #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test_format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: npm run build --if-present
# Commit changes
- name: Commit changes using git-auto-commit-action@v5
uses: stefanzweifel/git-auto-commit-action@v5
uses: stefanzweifel/git-auto-commit-action@v5.0.1
id: auto-commit-action
with:
commit_message: Prettier format
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,36 @@ npm install substack-feed-api
2. Import the API into your project:

```typescript
import { SubstackFeedApi } from 'substack-feed-api';
import { getSubstackFeed } from 'substack-feed-api';
```

### Usage

Here's a quick example to get you started:

```typescript
const api = new SubstackFeedApi();
getSubstackFeed('your-substack-newsletter-name')
.then((feed) => {
console.log(feed);
})
.catch((error) => {
console.error(error);
});
```

There is also a possibility to pass your own callback function to the `getSubstackFeed` function.
This function will be called with the parsed feed data as an argument:

api.fetchNewsletter('your-substack-newsletter-name').then(feed => {
console.info(feed);
}).catch(error => {
console.error('Error fetching newsletter:', error);
```typescript
getSubstackFeed('your-substack-newsletter-name', (err, result) => {
console.log(result);
});
```

You can also use the other exported functions from the package:
- `getFeedByLink`: Fetch a specific feed by its link.
- `getPosts`: Get all posts from a feed.

Replace `'your-substack-newsletter-name'` with the name of the Substack newsletter you wish to fetch.

## Development
Expand Down Expand Up @@ -87,4 +100,4 @@ Contributions are welcome! Feel free to open an issue or submit a pull request i
## License

This project is licensed under the MIT License.
```
```
19 changes: 12 additions & 7 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as parser from "xml2js";
import { RawFeedChannel, RawItem, SubstackItem } from "./types";
import { isRawFeed, isRawFeedChannel } from "./typeguards";
import { RawFeedChannel, RawItem, SubstackItem } from "./types";

const CORS_PROXY = "https://corsproxy.io/?";
const CORS_PROXY = "https://api.allorigins.win/get?url=";

// Internal API

const getRawXMLSubstackFeed = async (feedUrl: string) => {
const getRawXMLSubstackFeed = async (feedUrl: string, proxy = false) => {
try {
const promise = await fetch(`${CORS_PROXY}${encodeURIComponent(feedUrl)}`);
const path = proxy
? `${CORS_PROXY}${encodeURIComponent(feedUrl)}`
: feedUrl;
const promise = await fetch(path);
if (promise.ok) return promise.text();
} catch (e) {
throw new Error("Error occurred fetching Feed from Substack");
Expand Down Expand Up @@ -36,10 +39,12 @@ const transformRawItem = (item: RawItem): SubstackItem => {

export const getSubstackFeed = async (
feedUrl: string,
proxy = false,
/* eslint-disable @typescript-eslint/no-explicit-any */
callback: (err: Error | null, result: any) => void,
): Promise<void> => {
const rawXML = await getRawXMLSubstackFeed(feedUrl);
callback?: (err: Error | null, result: any) => void,
): Promise<string | undefined> => {
const rawXML = await getRawXMLSubstackFeed(feedUrl, proxy);
if (!callback) return rawXML;
await parseXML(rawXML, callback);
};
export const getFeedByLink = (
Expand Down
Loading