Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: refactor common types (#3449)
Browse files Browse the repository at this point in the history
Addresses #3442 (comment) by refactoring some of the common types used by root APIs as per #3413 

Co-authored-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
Gozala and achingbrain authored Dec 18, 2020
1 parent 18d6359 commit 34e1492
Show file tree
Hide file tree
Showing 53 changed files with 650 additions and 377 deletions.
1 change: 1 addition & 0 deletions examples/types-use-ipfs-from-ts/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default async function main() {

console.log('Added file:', file.path, file.cid.toString())
try {
// @ts-expect-error CID has no toUpperCase method
file.cid.toUpperCase()
} catch (error) {

Expand Down
1 change: 1 addition & 0 deletions examples/types-use-ipfs-from-typed-js/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function main () {

console.log('Added file:', file.path, file.cid.toString())
try {
// @ts-expect-error CID has no toUpperCase method
file.cid.toUpperCase()
} catch(error) {

Expand Down
5 changes: 5 additions & 0 deletions packages/ipfs-core-types/COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This project is transitioning from an MIT-only license to a dual MIT/Apache-2.0 license.
Unless otherwise noted, all code contributed prior to 2019-11-21 and not contributed by
a user listed in [this signoff issue](https://github.com/ipfs/js-ipfs/issues/2624) is
licensed under MIT-only. All new contributions (and past contributions since 2019-11-21)
are licensed under a dual MIT/Apache-2.0 license.
5 changes: 5 additions & 0 deletions packages/ipfs-core-types/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/ipfs-core-types/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
82 changes: 82 additions & 0 deletions packages/ipfs-core-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# ipfs-core-types <!-- omit in toc -->

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs/status.svg?style=flat-square&path=packages/interface-ipfs-core)](https://david-dm.org/ipfs/js-ipfs?path=packages/interface-ipfs-core)

> IPFS interface definitions used by implementations for API compatibility.
## Lead Maintainer <!-- omit in toc -->

[Alex Potsides](http://github.com/achingbrain)

## Table of Contents <!-- omit in toc -->

- [Background](#background)
- [Install](#install)
- [Usage](#usage)
- [Validation](#validation)
- [Contribute](#contribute)
- [Want to hack on IPFS?](#want-to-hack-on-ipfs)
- [License](#license)

## Background

The primary goal of this module is to define and ensure that IPFS core implementations and their respective HTTP client libraries implement the same interface, so that developers can quickly change between a local and a remote node without having to change their applications.

It offers set of typescript interface definitions that implementations can claim compatibility with and use typescript to validate those claims.

## Install

In JavaScript land:

```console
$ npm install ipfs-core-types
```

## Usage

Install `ipfs-core-types` as one of the dependencies of your project and use it to ensure your implementations API compatibility:

### In [JSDoc syntax](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html)

```js
/**
* @implements {import('ipfs-core-types').RootAPI}
*/
class Root {
// your implementation goes here
}
```

### In Typescript

```ts
import { RootAPI } from 'ipfs-core-types'
class Root implements RootAPI {
// your implementation goes here
}
```

## Validation

In order to validate API compatibility you can run [typescript](https://www.typescriptlang.org/) over your implementation which will point out all the API compatibilities if there are some.


## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs/issues)!

This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

### Want to hack on IPFS?

[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)

## License

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fipfs%2Fjs-ipfs.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fipfs%2Fjs-ipfs?ref=badge_large)

[![](https://github.com/ipfs/js-ipfs/raw/master/ipfs-core-types/img/badge.png)](https://github.com/ipfs/js-ipfs/tree/master/ipfs-core-types)
43 changes: 43 additions & 0 deletions packages/ipfs-core-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "ipfs-core-types",
"version": "0.1.0",
"description": "IPFS interface definitions used by implementations for API compatibility.",
"leadMaintainer": "Alex Potsides <alex@achingbrain.net>",
"types": "src/index.ts",
"homepage": "https://github.com/ipfs/js-ipfs/tree/master/packages/interface-ipfs-core#readme",
"bugs": "https://github.com/ipfs/js-ipfs/issues",
"scripts": {
"lint": "aegir lint",
"test": "aegir ts -p check"
},
"types": "src/index.ts",
"files": [
"src"
],
"eslintConfig": {
"extends": "ipfs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-ipfs.git"
},
"keywords": [
"IPFS",
"types",
"interface",
"API"
],
"license": "(Apache-2.0 OR MIT)",
"dependencies": {
"cids": "^1.0.0",
"multiaddr": "^8.0.0",
"peer-id": "^0.14.1"
},
"devDependencies": {
"aegir": "^29.2.2",
"typescript": "4.0.x"
},
"contributors": [
"Irakli Gozalishvili <dev@gozala.io>"
]
}
39 changes: 39 additions & 0 deletions packages/ipfs-core-types/src/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Represents a value that you can await on, which is either value or a promise
* of one.
*/
export type Await<T> =
| T
| Promise<T>

/**
* Represents an iterable that can be used in `for await` loops, that is either
* iterable or an async iterable.
*/
export type AwaitIterable<T> =
| Iterable<T>
| AsyncIterable<T>

/**
* Common options across all cancellable requests.
*/
export interface AbortOptions {
/**
* Can be provided to a function that starts a long running task, which will
* be aborted when signal is triggered.
*/
signal?: AbortSignal
/**
* Can be provided to a function that starts a long running task, which will
* be aborted after provided timeout (in ms).
*/
timeout?: number
}

export type ToJSON =
| null
| string
| number
| boolean
| ToJSON[]
| { toJSON?: () => ToJSON } & { [key: string]: ToJSON }
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PeerId, CID, Block, Await, BigInteger, AbortOptions } from './basic'
import { MovingAverage } from './moving-avarage'
import BigInteger from 'bignumber.js'
import PeerId from 'peer-id'
import CID from 'cids'
import { Block } from './block-service'
import { AbortOptions, Await } from './basic'
import { MovingAverage } from './bitswap/moving-avarage'
import { StoreReader, StoreExporter, StoreImporter } from './store'

export interface Bitswap extends
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Block, CID, Await, AbortOptions } from './basic'
import CID from 'cids'
import { Await, AbortOptions } from './basic'
import { StoreReader, StoreImporter, StoreExporter, StoreEraser } from './store'
import { Bitswap } from './bitswap'

Expand All @@ -18,3 +19,8 @@ export interface BlockService extends
*/
put(block: Block, options?:AbortOptions): Await<Block>
}

export interface Block {
cid: CID
data: Uint8Array
}
Loading

0 comments on commit 34e1492

Please sign in to comment.