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

feat: add example with Lit #12

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/browser-create-react-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Once the IPFS node is set up, `App.js` displays its ident and its version number

> _Side note: The PeerId of the IPFS node is [the multihash of the public key of this node](https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#peer-ids). The public-private key pair of a node is generated by typing `ipfs init`._

![Screen shot of the js ipfs node id info](./images/screenshot.png)
![Screen shot of the js ipfs node id info](./img/screenshot.png)

**Note**: this example is useful to learn how to spawn IPFS from a web page. It is also possible to [spawn an IPFS daemon from the command line](https://docs.ipfs.io/install/command-line/) with `ipfs daemon`. While self-hosting is advised, one can also delegate IPFS operations to a third-party like Infura. See tutorials [here](https://blog.infura.io/part-2-getting-started-with-ipfs-on-infura/) and [here](https://blog.infura.io/part-2-getting-started-with-ipfs-on-infura/).

Expand Down
105 changes: 105 additions & 0 deletions examples/browser-lit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<p align="center">
<a href="https://js.ipfs.io" title="JS IPFS">
<img src="https://ipfs.io/ipfs/Qme6KJdKcp85TYbLxuLV7oQzMiLremD7HMoXLZEmgo6Rnh/js-ipfs-sticker.png" alt="IPFS in JavaScript logo" width="244" />
</a>
</p>

<h3 align="center"><b>IPFS Lit App</b></h3>

<p align="center">
<b><i>Using js-ipfs with web components (lit)</i></b>
<br />
<br />
<img src="https://raw.githubusercontent.com/jlord/forkngo/gh-pages/badges/cobalt.png" width="200">
<br>
<a href="https://github.com/ipfs/js-ipfs/tree/master/docs">Explore the docs</a>
·
<a href="https://codesandbox.io/">View Demo</a>
·
<a href="https://github.com/ipfs-examples/js-ipfs-examples/issues">Report Bug</a>
·
<a href="https://github.com/ipfs-examples/js-ipfs-examples/issues">Request Feature/Example</a>
</p>

## Table of Contents

- [Table of Contents](#table-of-contents)
- [About The Project](#about-the-project)
- [Getting Started](#getting-started)
- [Pre requisites](#pre-requisites)
- [Installation and Running example](#installation-and-running-example)
- [Usage](#usage)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [Want to hack on IPFS?](#want-to-hack-on-ipfs)

## About The Project

- Read the [docs](https://github.com/ipfs/js-ipfs/tree/master/docs)
- Look into other [examples](https://github.com/ipfs/js-ipfs/tree/master/examples) to learn how to spawn an IPFS node in Node.js and in the Browser
- Consult the [Core API docs](https://github.com/ipfs/js-ipfs/tree/master/docs/core-api) to see what you can do with an IPFS node
- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
- Check out https://docs.ipfs.io for tips, how-tos and more
- See https://blog.ipfs.io for news and more
- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io

## Getting Started

### Pre requisites

Make sure you have installed all of the following prerequisites on your development machine:

- Git - [Download & Install Git](https://git-scm.com/downloads). OSX and Linux machines typically have this already installed.
- Node.js - [Download & Install Node.js](https://nodejs.org/en/download/) and the npm package manager.

### Installation and Running example

```console
> npm install
> npm start
```

Now open your browser at `http://localhost:8888`

## Usage

This is a minimal demonstration of how to use `js-ipfs` in a lit app.

![screenshot of the js ipfs node id info](./img/screenshot.png)

_For more examples, please refer to the [Documentation](#documentation)_

## Documentation

- [Config](https://docs.ipfs.io/)
- [Core API](https://github.com/ipfs/js-ipfs/tree/master/docs/core-api)
- [Examples](https://github.com/ipfs/js-ipfs/tree/master/examples)
- [Development](https://github.com/ipfs/js-ipfs/blob/master/docs/DEVELOPMENT.md)

## Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.

1. Fork the IPFS Project
2. Create your Feature Branch (`git checkout -b feature/amazing-feature`)
3. Commit your Changes (`git commit -a -m 'feat: add some amazing feature'`)
4. Push to the Branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## 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)

The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:

Read the [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md) and [JavaScript Contributing Guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md).

- **Check out existing issues** The [issue list](https://github.com/ipfs/js-ipfs/issues) has many that are marked as ['help wanted'](https://github.com/ipfs/js-ipfs/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22help+wanted%22) or ['difficulty:easy'](https://github.com/ipfs/js-ipfs/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Adifficulty%3Aeasy) which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
- **Look at the [IPFS Roadmap](https://github.com/ipfs/roadmap)** This are the high priority items being worked on right now
- **Perform code reviews** More eyes will help
a. speed the project along
b. ensure quality, and
c. reduce possible future bugs.
- **Add tests**. There can never be enough tests.
- **Join the [Weekly Core Implementations Call](https://github.com/ipfs/team-mgmt/issues/992)** it's where everyone discusses what's going on with IPFS and what's next
Binary file added examples/browser-lit/favicon.ico
Binary file not shown.
Binary file added examples/browser-lit/img/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions examples/browser-lit/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>js-ipfs with lit</title>

<link rel="shortcut icon" href="favicon.ico" />
<script defer type="module" src="./src/index.js"></script>
</head>
<body class="montserrat f5">
<header class="flex items-center pa3 bg-navy bb bw3 b--aqua">
<a href="https://ipfs.io" title="home">
<img
alt="IPFS logo"
src="./public/ipfs-logo.svg"
style="height: 50px"
class="v-top"
/>
</a>
</header>

<main class="pa4-l bg-snow mw7 mv5 center pa4">
<ipfs-info></ipfs-info>
</main>
</body>
</html>
30 changes: 30 additions & 0 deletions examples/browser-lit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "example-browser-lit",
"version": "1.0.0",
"private": true,
"description": "A minimal demonstration of how to use js-ipfs with lit",
"keywords": [],
"license": "MIT",
"scripts": {
"clean": "rimraf ./dist ./.cache ./.parcel-cache",
"build": "parcel build index.html --no-scope-hoist",
"serve": "parcel serve index.html --open -p 8888",
"start": "npm run serve",
"test": "npm run build && playwright test tests"
},
"browserslist": "last 1 Chrome version",
"dependencies": {
"ipfs": "^0.58.1",
"ipfs-css": "^1.3.0",
"lit": "^2.0.0-rc.2",
"tachyons": "^4.12.0"
},
"devDependencies": {
"@babel/core": "^7.14.8",
"@playwright/test": "^1.12.3",
"parcel": "latest",
"playwright": "^1.12.3",
"rimraf": "^3.0.2",
"test-util-ipfs-example": "^1.0.2"
}
}
1 change: 1 addition & 0 deletions examples/browser-lit/public/ipfs-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/browser-lit/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

// Global CSS
import "ipfs-css";
import 'tachyons'

// Components
import './ipfs'
72 changes: 72 additions & 0 deletions examples/browser-lit/src/ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'

import {html, LitElement} from 'lit';
import IPFS from 'ipfs'

export class IPFSInfo extends LitElement {
static get properties() {
return {
_ipfs: {state: true},
_id: {state: true},
_version: {state: true},
_agentVersion: {state: true}
}
}

constructor() {
super();

this._ipfs = null;
this._id = null;
this._version = null;
this._agentVersion = null;
}

connectedCallback() {
super.connectedCallback()

this.initIPFS();
}

async initIPFS() {
const ipfs = await IPFS.create();
const id = await ipfs.id();
const version = await ipfs.version();

this._ipfs = ipfs
this._id = id.id;
this._agentVersion = id.agentVersion;
this._version = version.version;
}

render() {
if (!this._ipfs) {
return html`Loading...`;
}

const info = {
'Id': this._id,
'Agent Version': this._agentVersion,
'Version': this._version,
}

return html`
<section>
<h1 data-test='title'>Connected to IPFS</h1>
<div>
${Object.entries(info).map(([key, value]) => {
return html`
<div>
<h2>${key}</h2>
<div data-test=${key.replace(/\s+/g, '')}>${value}</div>
</div>
`
}
)}
</div>
</section>
`;
}
}

customElements.define('ipfs-info', IPFSInfo);
28 changes: 28 additions & 0 deletions examples/browser-lit/tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { test, expect } = require('@playwright/test');
const { playwright } = require('test-util-ipfs-example');

// Setup
const play = test.extend({
...playwright.servers(),
});

play.describe('integrate ipfs with lit:', () => {
// DOM
const title = "[data-test=title]"
const id = "[data-test=Id]"
const agentVersion = "[data-test=AgentVersion]"
const version = "[data-test=Version]"

play.beforeEach(async ({servers, page}) => {
await page.goto(`http://localhost:${servers[0].port}/`);
})

play('should properly initialized a IPFS node and print some properties', async ({ page }) => {
await page.waitForSelector(title)

expect(await page.textContent(title)).toContain("Connected to IPFS");
expect(await page.isVisible(id)).toBeTruthy();
expect(await page.isVisible(agentVersion)).toBeTruthy();
expect(await page.isVisible(version)).toBeTruthy();
});
});