Skip to content

Commit

Permalink
fix: export classes & functions to avoid TS2304 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
flyfishzy committed Mar 2, 2022
1 parent 59e7c47 commit 27e22f0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios'
import {LambdaClient, InvokeCommand} from "@aws-sdk/client-lambda";
import {CognitoIdentityClient} from "@aws-sdk/client-cognito-identity";
import {fromCognitoIdentityPool} from "@aws-sdk/credential-provider-cognito-identity";
import { fetch, TextEncoder,TextDecoder } from 'openharmony-polyfill';

@Entry
@Component
Expand Down
11 changes: 11 additions & 0 deletions packages/polyfill/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
// mount polyfill objects to globalThis
import './web/index.js';
import './node/index.js';

// export classes & functions to avoid TS2304 errors
export { URL, URLSearchParams } from './web/url';
export { TextDecoder, TextEncoder } from './web/encoding';
export { XMLHttpRequest } from './web/xhr';
export { fetch, Headers, Request, Response } from './web/fetch';
export { crypto } from './web/crypto';
export { WebSocket } from './web/websocket';
export { LocalStorage } from './web/storage';
export { navigator } from './web/navigator';
37 changes: 21 additions & 16 deletions packages/polyfill/web/canvas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default class HmCanvas {
export class HmCanvas {
constructor(ctx, canvasId, canvasNode) {
this.ctx = ctx;
this.canvasId = canvasId;
Expand Down Expand Up @@ -46,22 +46,27 @@ export default class HmCanvas {

_initEvent() {
this.event = {};
const eventNames = [{
wxName: 'touchstart',
ecName: 'mousedown'
}, {
wxName: 'touchmove',
ecName: 'mousemove'
}, {
wxName: 'touchend',
ecName: 'mouseup'
}, {
wxName: 'touchend',
ecName: 'click'
}];
const eventNames = [
{
wxName: 'touchstart',
ecName: 'mousedown'
},
{
wxName: 'touchmove',
ecName: 'mousemove'
},
{
wxName: 'touchend',
ecName: 'mouseup'
},
{
wxName: 'touchend',
ecName: 'click'
}
];

eventNames.forEach(name => {
this.event[name.wxName] = e => {
eventNames.forEach((name) => {
this.event[name.wxName] = (e) => {
const touch = e.touches[0];
this.chart.getZr().handler.dispatch(name.ecName, {
zrX: touch.localX,
Expand Down
12 changes: 6 additions & 6 deletions packages/polyfill/web/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class ReadableStreamDefaultReader {
* Body class provides common methods for Request and Response
* @see https://fetch.spec.whatwg.org/#body
*/
export class Body {
export class Body {
#body;
#bodyUsed = false;
_bodyType;
Expand Down Expand Up @@ -353,7 +353,7 @@ class ReadableStreamDefaultReader {
/**
* The Request interface of the Fetch API represents a resource request.
*/
export class Request extends Body {
export class Request extends Body {
cache = 'default';

#url;
Expand All @@ -377,8 +377,8 @@ class ReadableStreamDefaultReader {
const inputBody = init.body
? init.body
: input instanceof Request
? input._body
: null;
? input._body
: null;
if ((method === 'GET' || method === 'HEAD') && inputBody) {
throw new TypeError('Body not allowed for GET or HEAD requests');
}
Expand Down Expand Up @@ -471,7 +471,7 @@ class ReadableStreamDefaultReader {
/**
* The Response interface of the Fetch API represents the response to a request.
*/
export class Response extends Body {
export class Response extends Body {
#type;
#status;
#ok;
Expand Down Expand Up @@ -625,7 +625,7 @@ function getHarmonyRequestOptions(request) {
* An object containing any custom settings that you want to apply to the request.
* @returns
*/
export function fetch(resource, init) {
export function fetch(resource, init = {}) {
return new Promise(function (resolve, reject) {
const request = new Request(resource, init);
if (request.signal && request.signal.aborted) {
Expand Down
25 changes: 24 additions & 1 deletion packages/webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ OpenHarmony Polyfill in Webpack.
```
npm install @originjs/openharmony-webpack-plugin
```
at `{YOUR_SDK_PATH}\Sdk\ets\3.0.0.0\build-tools\ets-loader`
## Usage

Add the following to your `webpack.config.js`: (at `{YOUR_SDK_PATH}\Sdk\ets\3.0.0.0\build-tools\ets-loader`)
Expand Down Expand Up @@ -52,4 +53,26 @@ module.exports = {
- `stream`
- `url`
- `util`
- `fs`
- `fs`

## FAQ
### TS2304: Cannot find name 'xx'
If you use these global API directly in `ets`, it may appear an TS2304 error. eg.
```
ETS:ERROR File: examples\sdk7-demo\entry\src\main\ets\default\pages\textutil.ets:14:29
TS2304: Cannot find name 'TextDecoder'.
ETS:ERROR File: examples\sdk7-demo\entry\src\main\ets\default\pages\textutil.ets:31:29
TS2304: Cannot find name 'TextEncoder'.
ETS:ERROR File: examples\sdk7-demo\entry\src\main\ets\default\pages\textutil.ets:45:25
TS2304: Cannot find name 'URL'.
```

**Solutions:**
Add import statements:
```js
import { TextDecoder } from 'openharmony-polyfill';
import { TextEncoder } from 'openharmony-polyfill';
import { URL } from 'openharmony-polyfill';
```

0 comments on commit 27e22f0

Please sign in to comment.