-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Split enhanceEndpoints
into addTagTypes
and enhanceEndpoint
#3485
Open
EskiMojo14
wants to merge
32
commits into
master
Choose a base branch
from
enhance-endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
53d0626
create separate addTagTypes function
EskiMojo14 93f8b4e
add enhanceEndpoint
EskiMojo14 60b2efc
omit base api methods from module requirements
EskiMojo14 ee02202
update codegen and type tests
EskiMojo14 8d2b42e
update codemod deps
EskiMojo14 16ab2b8
add codemod
EskiMojo14 cc406db
Merge branch 'v2.0-integration' into enhance-endpoint
EskiMojo14 36187be
fix APIProvider
EskiMojo14 12d8720
simplify typing using ApiModules instead of Omit<Api, keyof BaseApiMe…
EskiMojo14 81f187e
fix apiprovider type
EskiMojo14 b65f6b5
update docs and remove enhanceEndpoints from typing (runtime fallback…
EskiMojo14 8c38f92
use promiseWithResolvers for RTKQ lifecycle management
319c772
require reason to ensure onrejected callback type is accurate
EskiMojo14 8833de4
export promiseWithResolvers from utils index
EskiMojo14 7e3f4a5
use utils index
EskiMojo14 bb7451c
add promiseWithResolvers test
EskiMojo14 47eeb8c
Merge branch 'v2.0-integration' into enhance-endpoint
fb8a1aa
lockfile
b683ccb
Merge branch 'master' into enhance-endpoint
EskiMojo14 948f6d9
run prettier
EskiMojo14 c5da068
fix tests to avoid enhanceEndpoints
EskiMojo14 31707eb
fix fixedCacheKey test
EskiMojo14 49698cd
add enhanceEndpoints back as deprecated
EskiMojo14 fa4f0e4
add the types as well
EskiMojo14 7e53c8e
try something
EskiMojo14 273139c
Merge branch 'master' into enhance-endpoint
EskiMojo14 119abc9
Merge branch 'master' into enhance-endpoint
EskiMojo14 ceff137
Merge branch 'master' into enhance-endpoint
EskiMojo14 386e24b
Merge branch 'master' into enhance-endpoint
EskiMojo14 41cded6
Merge branch 'master' into enhance-endpoint
EskiMojo14 dc08e93
update README version
EskiMojo14 675f261
put code on one line, to account for error in different versions
EskiMojo14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/rtk-codemods/transforms/enhanceEndpoint/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# enhanceEndpoint | ||
|
||
Transforms enhanceEndpoints calls into addTagType and enhanceEndpoint calls, in preparation for RTK 3.0. | ||
|
||
## Usage | ||
|
||
``` | ||
npx @reduxjs/rtk-codemods enhanceEndpoint path/of/files/ or/some**/*glob.js | ||
|
||
# or | ||
|
||
yarn global add @reduxjs/rtk-codemods | ||
@reduxjs/rtk-codemods enhanceEndpoint path/of/files/ or/some**/*glob.js | ||
``` | ||
|
||
## Local Usage | ||
|
||
``` | ||
node ./bin/cli.js enhanceEndpoint path/of/files/ or/some**/*glob.js | ||
``` | ||
|
||
## Input / Output | ||
|
||
<!--FIXTURES_TOC_START--> | ||
<!--FIXTURES_TOC_END--> | ||
|
||
<!--FIXTURES_CONTENT_START--> | ||
<!--FIXTURES_CONTENT_END--> |
38 changes: 38 additions & 0 deletions
38
packages/rtk-codemods/transforms/enhanceEndpoint/__testfixtures__/basic.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const withTags = api.enhanceEndpoints({ | ||
addTagTypes: ['tag1'] | ||
}); | ||
|
||
const mutation2 = 'mutation2'; | ||
|
||
const withPartials = withTags.enhanceEndpoints({ | ||
endpoints: { | ||
query1: { | ||
providesTags: ['tag1'] | ||
}, | ||
mutation1(definition) { | ||
definition.invalidatesTags = ['tag1'] | ||
}, | ||
[mutation2]: (definition) => {} | ||
} | ||
}) | ||
|
||
const tags = ['tag1'] | ||
|
||
const withBoth = api.enhanceEndpoints({ | ||
addTagTypes: tags, | ||
endpoints: { | ||
["query1"]: { | ||
providesTags: ['tag1'] | ||
}, | ||
} | ||
}) | ||
|
||
const addTagTypes = tags | ||
|
||
const chained = api | ||
.enhanceEndpoints({ | ||
addTagTypes | ||
}) | ||
.injectEndpoints({ | ||
endpoints: () => {} | ||
}) |
22 changes: 22 additions & 0 deletions
22
packages/rtk-codemods/transforms/enhanceEndpoint/__testfixtures__/basic.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const withTags = api.addTagTypes('tag1'); | ||
|
||
const mutation2 = 'mutation2'; | ||
|
||
const withPartials = withTags.enhanceEndpoint("query1", { | ||
providesTags: ['tag1'] | ||
}).enhanceEndpoint("mutation1", (definition) => { | ||
definition.invalidatesTags = ['tag1'] | ||
}).enhanceEndpoint(mutation2, (definition) => {}) | ||
|
||
const tags = ['tag1'] | ||
|
||
const withBoth = api.addTagTypes(...tags).enhanceEndpoint("query1", { | ||
providesTags: ['tag1'] | ||
}) | ||
|
||
const addTagTypes = tags | ||
|
||
const chained = api.addTagTypes(...addTagTypes) | ||
.injectEndpoints({ | ||
endpoints: () => {} | ||
}) |
126 changes: 126 additions & 0 deletions
126
packages/rtk-codemods/transforms/enhanceEndpoint/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import type { | ||
Transform, | ||
MemberExpression, | ||
ObjectExpression, | ||
RestElement, | ||
JSCodeshift | ||
} from 'jscodeshift' | ||
|
||
type Prop = Extract< | ||
ObjectExpression['properties'][number], | ||
{ value: unknown } | ||
>['value'] | ||
|
||
const createAddTagTypesCall = ( | ||
j: JSCodeshift, | ||
object: MemberExpression['object'], | ||
addTagTypes: Prop | ||
) => { | ||
const newCall = j.callExpression( | ||
j.memberExpression(object, j.identifier('addTagTypes')), | ||
[] | ||
) | ||
if (addTagTypes.type === 'Identifier') { | ||
newCall.arguments.push(j.spreadElement(addTagTypes)) | ||
} else if (addTagTypes.type === 'ArrayExpression') { | ||
newCall.arguments = addTagTypes.elements.filter( | ||
(el): el is Exclude<typeof el, RestElement | null> => | ||
!!(el && el.type !== 'RestElement') | ||
) | ||
} | ||
return newCall | ||
} | ||
|
||
const transform: Transform = (file, api) => { | ||
const j = api.jscodeshift | ||
|
||
const root = j(file.source) | ||
|
||
return root | ||
.find(j.CallExpression, { | ||
callee: { | ||
property: { | ||
name: 'enhanceEndpoints' | ||
} | ||
} | ||
}) | ||
.forEach((path) => { | ||
const callee = path.value.callee as MemberExpression | ||
const [config] = path.value.arguments | ||
if (config.type === 'ObjectExpression') { | ||
let addTagTypes: Prop | undefined = undefined | ||
let endpoints: Prop | undefined = undefined | ||
for (const property of config.properties) { | ||
if ( | ||
(property.type === 'ObjectProperty' || | ||
property.type === 'Property') && | ||
property.key.type === 'Identifier' | ||
) { | ||
switch (property.key.name) { | ||
case 'addTagTypes': | ||
addTagTypes = property.value | ||
break | ||
case 'endpoints': | ||
endpoints = property.value | ||
break | ||
} | ||
} | ||
} | ||
if (!endpoints) { | ||
if (!addTagTypes) { | ||
return | ||
} | ||
// no endpoints - we can go ahead and replace | ||
path.replace(createAddTagTypesCall(j, callee.object, addTagTypes)) | ||
} else { | ||
let calleeObject = addTagTypes | ||
? createAddTagTypesCall(j, callee.object, addTagTypes) | ||
: callee.object | ||
if (endpoints.type === 'ObjectExpression') { | ||
for (const endpointProp of endpoints.properties) { | ||
if (endpointProp.type === 'ObjectProperty') { | ||
const endpointName = | ||
endpointProp.key.type === 'Identifier' && | ||
!endpointProp.computed | ||
? j.stringLiteral(endpointProp.key.name) | ||
: endpointProp.key | ||
calleeObject = j.callExpression( | ||
j.memberExpression( | ||
calleeObject, | ||
j.identifier('enhanceEndpoint') | ||
), | ||
[endpointName, endpointProp.value as any] | ||
) | ||
} else if (endpointProp.type === 'ObjectMethod') { | ||
const endpointName = | ||
endpointProp.key.type === 'Identifier' | ||
? j.stringLiteral(endpointProp.key.name) | ||
: endpointProp.key | ||
calleeObject = j.callExpression( | ||
j.memberExpression( | ||
calleeObject, | ||
j.identifier('enhanceEndpoint') | ||
), | ||
[ | ||
endpointName, | ||
j.arrowFunctionExpression( | ||
endpointProp.params, | ||
endpointProp.body | ||
) | ||
] | ||
) | ||
} | ||
} | ||
} | ||
path.replace(calleeObject) | ||
} | ||
} | ||
}) | ||
.toSource({ | ||
arrowParensAlways: true | ||
}) | ||
} | ||
|
||
export const parser = 'tsx' | ||
|
||
export default transform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
const { runTransformTest } = require('codemod-cli') | ||
|
||
runTransformTest({ | ||
name: 'enhanceEndpoint', | ||
path: require.resolve('./index.ts'), | ||
fixtureDir: `${__dirname}/__testfixtures__/` | ||
}) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably get rid of
codemod-cli
since it hasn't been updated in about 4 years and does not work with later versions of TypeScript.