Skip to content

Commit

Permalink
feat: updated getSubstackFeedByLink method signature
Browse files Browse the repository at this point in the history
now returns raw XML string when no callback is passed
  • Loading branch information
rohit1901 committed Feb 7, 2025
1 parent 4e1ae1d commit d55bef4
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 366 deletions.
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

0 comments on commit d55bef4

Please sign in to comment.