Skip to content

Commit

Permalink
Merge branch 'main' into better-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Huskydog9988 authored Sep 29, 2023
2 parents 96dcfa8 + 0090fa5 commit 65f6f24
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/update-lockfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://github.com/dependabot/dependabot-core/issues/1736
name: Dependabot
on: pull_request_target
permissions: read-all
jobs:
update-lockfile:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
permissions:
pull-requests: write
contents: write
steps:
- uses: pnpm/action-setup@v2
with:
version: ^7
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- run: pnpm i --lockfile-only
- run: |
git config --global user.name github-actions[bot]
git config --global user.email github-actions[bot]@users.noreply.github.com
git add pnpm-lock.yaml
git commit -m "Update pnpm-lock.yaml"
git push
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "better-trakt",
"version": "0.9.0",
"version": "0.9.1",
"description": "A Trakt.tv client with native Typescript support and quality of life features",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -30,11 +30,11 @@
"test": "nyc mocha -r ts-node/register test/**/*.spec.ts"
},
"dependencies": {
"axios": "^0.27.2"
"axios": "^1.1.3"
},
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.1",
"@types/mocha": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"chai": "^4.3.6",
Expand All @@ -50,7 +50,7 @@
"typedoc": "^0.23.10",
"typedoc-plugin-extras": "^2.3.0",
"typedoc-plugin-mdn-links": "^2.0.0",
"typedoc-plugin-missing-exports": "^0.23.0",
"typedoc-plugin-missing-exports": "^1.0.0",
"typescript": "^4.7.4"
},
"publishConfig": {
Expand Down
1 change: 1 addition & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/trakt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './users';
* @private
*/
export type MediaNamespace = 'movies' | 'shows';
export * from './episodes';
69 changes: 46 additions & 23 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosInstance, AxiosRequestHeaders, AxiosResponseHeaders } from 'axios';
import { AxiosError, AxiosInstance, AxiosRequestHeaders, AxiosResponseHeaders } from 'axios';
import { CommentSortByMedia, ListQueryByType, RecommendedPeriod, ReleasesCountry, UpdatedStartDate } from '../trakt';
import { buildUrl } from './buildUrl';

Expand Down Expand Up @@ -94,7 +94,12 @@ export interface ApiResponse<T> {
* @remarks
* Can be useful for things like cache control, ratelimiting, or general debuging
*/
headers: AxiosResponseHeaders;
headers: AxiosResponseHeaders | Partial<Record<string, string> & { 'set-cookie'?: string[] | undefined }>;

/**
* Error object in the event of an error
*/
error?: TraktHttpError;
}

export interface FetchOptions {
Expand Down Expand Up @@ -148,28 +153,46 @@ export interface FetchOptions {
* @internal
*/
export async function fetch<T>(client: AxiosInstance, url: string, options?: FetchOptions): Promise<ApiResponse<T>> {
const headers: AxiosRequestHeaders = {};

if (options !== undefined && options.accessToken !== undefined)
headers['Authorization'] = `Bearer ${options.accessToken}`;

const response = await client.get<T>(buildUrl(url, options), {
headers,
// parseJson: (text: string) => Bourne.parse(text),
});

const res: ApiResponse<T> = {
data: response.data,
headers: response.headers,
};

if (response.headers['X-Pagination-Page'] !== undefined) {
res.pagination = {
page: parseInt(response.headers['X-Pagination-Page']),
limit: parseInt(response.headers['X-Pagination-Limit']),
pageCount: parseInt(response.headers['X-Pagination-Page-Count']),
itemCount: parseInt(response.headers['X-Pagination-Item-Count']),
try {
const response = await client.get<T>(buildUrl(url, options), {
headers:
options !== undefined && options.accessToken !== undefined
? { Authorization: `Bearer ${options.accessToken}` }
: undefined,
// parseJson: (text: string) => Bourne.parse(text),
});

const res: ApiResponse<T> = {
data: response.data,
headers: response.headers,
};

if (
response.headers['X-Pagination-Page'] !== undefined &&
response.headers['X-Pagination-Limit'] !== undefined &&
response.headers['X-Pagination-Page-Count'] !== undefined &&
response.headers['X-Pagination-Item-Count'] !== undefined
) {
res.pagination = {
page: parseInt(response.headers['X-Pagination-Page']),
limit: parseInt(response.headers['X-Pagination-Limit']),
pageCount: parseInt(response.headers['X-Pagination-Page-Count']),
itemCount: parseInt(response.headers['X-Pagination-Item-Count']),
};
}

return res;
} catch (e) {
if (e instanceof AxiosError && e.response !== undefined) {
// throw new TraktHttpError(e.response.status, e.response.data, e.response.headers);
return {
error: new TraktHttpError(e.response.status, e.response.data, e.response.headers),
headers: e.response.headers,
};
}

// console.error(e);
throw e;
}

return res;
Expand Down

0 comments on commit 65f6f24

Please sign in to comment.