-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Convert wp data controls to typescript #49647
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
import triggerFetch from '@wordpress/api-fetch'; | ||
import { controls as dataControls } from '@wordpress/data'; | ||
import deprecated from '@wordpress/deprecated'; | ||
import type { StoreDescriptor } from '@wordpress/data'; | ||
import type { APIFetchOptions } from '@wordpress/api-fetch'; | ||
|
||
/** | ||
* Dispatches a control action for triggering an api fetch call. | ||
|
@@ -24,7 +26,7 @@ import deprecated from '@wordpress/deprecated'; | |
* | ||
* @return {Object} The control descriptor. | ||
*/ | ||
export function apiFetch( request ) { | ||
export function apiFetch( request: APIFetchOptions ) { | ||
return { | ||
type: 'API_FETCH', | ||
request, | ||
|
@@ -35,45 +37,67 @@ export function apiFetch( request ) { | |
* Control for resolving a selector in a registered data store. | ||
* Alias for the `resolveSelect` built-in control in the `@wordpress/data` package. | ||
* | ||
* @param {Array} args Arguments passed without change to the `@wordpress/data` control. | ||
* @param storeNameOrDescriptor The store object or identifier. | ||
* @param selectorName The selector name. | ||
* @param args Arguments passed without change to the `@wordpress/data` control. | ||
*/ | ||
export function select( ...args ) { | ||
export function select( | ||
storeNameOrDescriptor: string | StoreDescriptor, | ||
selectorName: string, | ||
...args: any[] | ||
) { | ||
deprecated( '`select` control in `@wordpress/data-controls`', { | ||
since: '5.7', | ||
alternative: 'built-in `resolveSelect` control in `@wordpress/data`', | ||
} ); | ||
|
||
return dataControls.resolveSelect( ...args ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typescript wasn't happy about this because it's not specific enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it, it's way more predictable that way. |
||
return dataControls.resolveSelect( | ||
storeNameOrDescriptor, | ||
selectorName, | ||
...args | ||
); | ||
} | ||
|
||
/** | ||
* Control for calling a selector in a registered data store. | ||
* Alias for the `select` built-in control in the `@wordpress/data` package. | ||
* | ||
* @param {Array} args Arguments passed without change to the `@wordpress/data` control. | ||
* @param storeNameOrDescriptor The store object or identifier. | ||
* @param selectorName The selector name. | ||
* @param args Arguments passed without change to the `@wordpress/data` control. | ||
*/ | ||
export function syncSelect( ...args ) { | ||
export function syncSelect( | ||
storeNameOrDescriptor: string | StoreDescriptor, | ||
selectorName: string, | ||
...args: any[] | ||
) { | ||
deprecated( '`syncSelect` control in `@wordpress/data-controls`', { | ||
since: '5.7', | ||
alternative: 'built-in `select` control in `@wordpress/data`', | ||
} ); | ||
|
||
return dataControls.select( ...args ); | ||
return dataControls.select( storeNameOrDescriptor, selectorName, ...args ); | ||
} | ||
|
||
/** | ||
* Control for dispatching an action in a registered data store. | ||
* Alias for the `dispatch` control in the `@wordpress/data` package. | ||
* | ||
* @param {Array} args Arguments passed without change to the `@wordpress/data` control. | ||
* @param storeNameOrDescriptor The store object or identifier. | ||
* @param actionName The action name. | ||
* @param args Arguments passed without change to the `@wordpress/data` control. | ||
*/ | ||
export function dispatch( ...args ) { | ||
export function dispatch( | ||
storeNameOrDescriptor: string | StoreDescriptor, | ||
actionName: string, | ||
...args: any[] | ||
) { | ||
deprecated( '`dispatch` control in `@wordpress/data-controls`', { | ||
since: '5.7', | ||
alternative: 'built-in `dispatch` control in `@wordpress/data`', | ||
} ); | ||
|
||
return dataControls.dispatch( ...args ); | ||
return dataControls.dispatch( storeNameOrDescriptor, actionName, ...args ); | ||
} | ||
|
||
/** | ||
|
@@ -95,7 +119,7 @@ export function dispatch( ...args ) { | |
* | ||
* @return {Object} The control descriptor. | ||
*/ | ||
export const __unstableAwaitPromise = function ( promise ) { | ||
export const __unstableAwaitPromise = function < T >( promise: Promise< T > ) { | ||
return { | ||
type: 'AWAIT_PROMISE', | ||
promise, | ||
|
@@ -130,8 +154,8 @@ export const __unstableAwaitPromise = function ( promise ) { | |
* store. | ||
*/ | ||
export const controls = { | ||
AWAIT_PROMISE: ( { promise } ) => promise, | ||
API_FETCH( { request } ) { | ||
AWAIT_PROMISE: < T >( { promise }: { promise: Promise< T > } ) => promise, | ||
API_FETCH( { request }: { request: APIFetchOptions } ) { | ||
return triggerFetch( request ); | ||
}, | ||
}; |
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,14 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"declarationDir": "build-types", | ||
"types": [ "gutenberg-env" ] | ||
}, | ||
"references": [ | ||
{ "path": "../api-fetch" }, | ||
{ "path": "../data" }, | ||
{ "path": "../deprecated" } | ||
], | ||
"include": [ "src/**/*" ] | ||
} |
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
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.
APIFetchOptions is what DT uses for this