Skip to content

Commit

Permalink
fix: Improve getParam usage with custom error (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts authored Apr 17, 2021
1 parent 3dd43b7 commit d64e402
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-meals-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-source-tmdb": patch
---

fix: Improve getParam usage with custom error
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ on:
push:
branches:
- main
paths:
- "package/**"
jobs:
release:
name: Release
Expand All @@ -14,6 +12,10 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: "**/node_modules"
key: ${{ runner.os }}-12.x-modules-${{ hashFiles('**/yarn.lock') }}
- name: Setup Node.js 12.x
uses: actions/setup-node@v1
with:
Expand Down
4 changes: 4 additions & 0 deletions package/src/__tests__/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe(`api-utils`, () => {
})
it(`getParam should capture param`, () => {
expect(getParam(`/tv/:tv_id`)).toBe(`tv_id`)
expect(getParam(`/tv/1234`)).toBe(undefined)
})
it(`defaultOptions works correctly`, () => {
expect(defaultOptions({ apiKey: `foo`, sessionID: `bar`, plugins: [] })).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -79,6 +80,9 @@ describe(`api-utils`, () => {
it(`generateTypeName works correctly`, () => {
expect(generateTypeName({ url: `account/:account_id/favorite/movies` }, `Tmdb`)).toBe(`TmdbAccountFavoriteMovies`)
expect(generateTypeName({ url: `tv/:tv_id`, context: { tv_id: `1234` } }, `Tmdb`)).toBe(`TmdbTv1234`)
expect(generateTypeName({ url: `tv/:tv_id`, context: { tv_id: `1234` }, typeName: `CustomName` }, `Tmdb`)).toBe(
`TmdbCustomName`
)
expect(generateTypeName({ url: `account/:account_id/favorite/movies`, typeName: `CustomName` }, `Tmdb`)).toBe(
`TmdbCustomName`
)
Expand Down
2 changes: 1 addition & 1 deletion package/src/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getParamRegex = /:(.*)/
* @param url
* @return Parameter without colon
*/
export const getParam = (url: string): string => url.match(getParamRegex)[1]
export const getParam = (url: string): string => url.match(getParamRegex)?.[1]

/**
* Combine the user plugin options with the default options
Expand Down
1 change: 1 addition & 0 deletions package/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const ERROR_CODES = {
initialSourcing: `30000`,
individualSourcing: `30001`,
extensionSourcing: `30002`,
getParamUndefined: `30003`,
}
export const IMAGE_TYPES = [`backdrop`, `logo`, `poster`, `profile`, `still`] as const
export const IMAGE_TYPE_NAMES = [`BackdropPath`, `LogoPath`, `PosterPath`, `ProfilePath`, `StillPath`] as const
Expand Down
11 changes: 11 additions & 0 deletions package/src/node-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ export const nodeBuilder = async ({
if (endpoint.extension) {
try {
const param = getParam(endpoint.extension.url)

if (!param) {
gatsbyApi.reporter.panicOnBuild({
id: ERROR_CODES.getParamUndefined,
context: {
sourceMessage: `Couldn't find a parameter for ${endpoint.extension.url} - make sure it uses the format :your_parameter`,
},
})
itemTimer.end()
}

const detailedItems = items.map((item) =>
fetchData({ url: endpoint.extension.url, context: { [param]: item.id } })
)
Expand Down

0 comments on commit d64e402

Please sign in to comment.