Skip to content

Commit

Permalink
Convert to TypeScript (#187)
Browse files Browse the repository at this point in the history
This converts the code base to TypeScript. `Transloadit` is now a named export instead of being exported via `module.exports =`.

---------

Co-authored-by: Mikael Finstad <[email protected]>
  • Loading branch information
remcohaszing and mifi authored Oct 11, 2024
1 parent ba79187 commit 3002ded
Show file tree
Hide file tree
Showing 28 changed files with 997 additions and 1,296 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
coverage/
dist/
node_modules/
fixture/
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ module.exports = {
ecmaVersion: 11,
requireConfigFile: false,
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.ts'],
},
},
},
rules: {
'import/extensions': 'off',
},
overrides: [
{
files: 'test/**',
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ jobs:
- run: corepack yarn
- run: corepack yarn prettier --check .

tsd:
typescript:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: corepack yarn
- run: corepack yarn tsd
- run: corepack yarn tsc --build

vitest:
runs-on: ubuntu-latest
Expand All @@ -73,7 +73,7 @@ jobs:
- eslint
- pack
- prettier
- tsd
- typescript
- vitest
if: startsWith(github.ref, 'refs/tags/')
permissions:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
dist/
node_modules
cloudflared*
credentials.js
sample.js

*.tsbuildinfo
npm-debug.log
env.sh
/coverage
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage/
dist/
fixture/
node_modules/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ npm install --save transloadit
The following code will upload an image and resize it to a thumbnail:

```javascript
const Transloadit = require('transloadit')
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: 'YOUR_TRANSLOADIT_KEY',
Expand Down Expand Up @@ -113,7 +113,7 @@ For more example use cases and information about the available robots and their

## API

These are the public methods on the `Transloadit` object and their descriptions. The methods are based on the [Transloadit API](https://transloadit.com/docs/api/). See also [TypeScript definitions](types/index.d.ts).
These are the public methods on the `Transloadit` object and their descriptions. The methods are based on the [Transloadit API](https://transloadit.com/docs/api/).

Table of contents:

Expand Down Expand Up @@ -391,7 +391,7 @@ This function returns an object with the key `signature` (containing the calcula

### Errors

Errors from Node.js will be passed on and we use [GOT](https://github.com/sindresorhus/got) for HTTP requests and errors from there will also be passed on. When the HTTP response code is not 200, the error will be a `Transloadit.HTTPError`, which is a [got.HTTPError](https://github.com/sindresorhus/got#errors)) with some additional properties:
Errors from Node.js will be passed on and we use [GOT](https://github.com/sindresorhus/got) for HTTP requests and errors from there will also be passed on. When the HTTP response code is not 200, the error will be an `HTTPError`, which is a [got.HTTPError](https://github.com/sindresorhus/got#errors)) with some additional properties:

- `HTTPError.response?.body` the JSON object returned by the server along with the error response (**note**: `HTTPError.response` will be `undefined` for non-server errors)
- `HTTPError.transloaditErrorCode` alias for `HTTPError.response.body.error` ([View all error codes](https://transloadit.com/docs/api/response-codes/#error-codes))
Expand All @@ -401,7 +401,7 @@ To identify errors you can either check its props or use `instanceof`, e.g.:

```js
catch (err) {
if (err instanceof Transloadit.TimeoutError) {
if (err instanceof TimeoutError) {
return console.error('The request timed out', err)
}
if (err.code === 'ENOENT') {
Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"@babel/core": "^7.12.3",
"@babel/eslint-parser": "^7.15.8",
"@babel/eslint-plugin": "^7.13.10",
"@types/debug": "^4.1.12",
"@types/temp": "^0.9.4",
"@vitest/coverage-v8": "^2.0.5",
"badge-maker": "^3.3.0",
"eslint": "^7.18.0",
Expand All @@ -46,7 +48,7 @@
"p-retry": "^4.2.0",
"prettier": "^2.8.6",
"temp": "^0.9.1",
"tsd": "^0.25.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
"repository": {
Expand All @@ -62,20 +64,22 @@
"lint": "npm-run-all --parallel 'lint:*'",
"fix": "npm-run-all --serial 'fix:*'",
"next:update": "next-update --keep true --tldr",
"prepack": "tsc --build",
"test-unit": "vitest run --coverage ./test/unit",
"test-integration": "vitest run ./test/integration",
"tsd": "tsd",
"test-all": "npm run tsd && vitest run --coverage",
"test": "npm run tsd && npm run test-unit",
"fix:formatting": "prettier --write .",
"lint:formatting": "prettier --check ."
},
"license": "MIT",
"main": "./index",
"types": "types/index.d.ts",
"main": "./dist/Transloadit.js",
"exports": {
".": "./dist/Transloadit.js",
"./package.json": "./package.json"
},
"files": [
"index.js",
"src",
"types/index.d.ts"
"dist",
"src"
]
}
5 changes: 0 additions & 5 deletions src/InconsistentResponseError.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/InconsistentResponseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class InconsistentResponseError extends Error {
override name = 'InconsistentResponseError'
}
25 changes: 14 additions & 11 deletions src/PaginationStream.js → src/PaginationStream.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const stream = require('stream')
import { Readable } from 'stream'
import { PaginationList } from './Transloadit'

class PaginationStream extends stream.Readable {
constructor(_fetchPage) {
type FetchPage<T> = (pageno: number) => PaginationList<T> | PromiseLike<PaginationList<T>>

export class PaginationStream<T> extends Readable {
private _fetchPage: FetchPage<T>
private _nitems?: number
private _pageno = 0
private _items: T[] = []
private _itemsRead = 0

constructor(fetchPage: FetchPage<T>) {
super({ objectMode: true })
this._fetchPage = _fetchPage
this._pageno = 0
this._items = []
this._itemsRead = 0
this._fetchPage = fetchPage
}

async _read() {
override async _read() {
if (this._items.length > 0) {
this._itemsRead++
process.nextTick(() => this.push(this._items.pop()))
Expand All @@ -29,11 +35,8 @@ class PaginationStream extends stream.Readable {
this._items.reverse()

this._read()
return
} catch (err) {
this.emit('error', err)
}
}
}

module.exports = PaginationStream
7 changes: 0 additions & 7 deletions src/PollingTimeoutError.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/PollingTimeoutError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class PollingTimeoutError extends Error {
override name = 'PollingTimeoutError'
code = 'POLLING_TIMED_OUT'
}
Loading

0 comments on commit 3002ded

Please sign in to comment.