Skip to content

Commit

Permalink
Convert wp data controls to typescript (#49647)
Browse files Browse the repository at this point in the history
* Convert WordPress data controls to typescript
  • Loading branch information
noahtallen authored Apr 11, 2023
1 parent bae0d6b commit 423976a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/data-controls/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking Changes

- Publish Typescript build types to npm. ([#49647](https://github.com/WordPress/gutenberg/pull/49647))

## 2.30.0 (2023-03-29)

## 2.29.0 (2023-03-15)
Expand Down
12 changes: 9 additions & 3 deletions packages/data-controls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,29 @@ Control for dispatching an action in a registered data store. Alias for the `dis

_Parameters_

- _args_ `Array`: Arguments passed without change to the `@wordpress/data` control.
- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier.
- _actionName_ `string`: The action name.
- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control.

### select

Control for resolving a selector in a registered data store. Alias for the `resolveSelect` built-in control in the `@wordpress/data` package.

_Parameters_

- _args_ `Array`: Arguments passed without change to the `@wordpress/data` control.
- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier.
- _selectorName_ `string`: The selector name.
- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control.

### syncSelect

Control for calling a selector in a registered data store. Alias for the `select` built-in control in the `@wordpress/data` package.

_Parameters_

- _args_ `Array`: Arguments passed without change to the `@wordpress/data` control.
- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier.
- _selectorName_ `string`: The selector name.
- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control.

<!-- END TOKEN(Autogenerated API docs) -->

Expand Down
1 change: 1 addition & 0 deletions packages/data-controls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/api-fetch": "file:../api-fetch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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 );
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 );
}

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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 );
},
};
14 changes: 14 additions & 0 deletions packages/data-controls/tsconfig.json
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/**/*" ]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{ "path": "packages/compose" },
{ "path": "packages/core-data" },
{ "path": "packages/data" },
{ "path": "packages/data-controls" },
{ "path": "packages/date" },
{ "path": "packages/deprecated" },
{ "path": "packages/docgen" },
Expand Down

1 comment on commit 423976a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 423976a.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4671340364
📝 Reported issues:

Please sign in to comment.