Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎉 hyperdata #163

Merged
merged 2 commits into from
Sep 3, 2023
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

Monorepository of npm packages we use in our projects.

## 📦 hyperdata

[![hyperdata version][hyperdata-version-src]][hyperdata-version-href]

Library for working with NFT metadata.

## 📦 if-that

[![if-that version][if-that-version-src]][if-that-version-href]
Expand Down Expand Up @@ -36,6 +42,9 @@ Logical and friendly wrapper for Substrate API (@polkadot).


<!-- Badges -->
[hyperdata-version-src]: https://img.shields.io/npm/v/@kodadot1/hyperdata/latest.svg?style=flat&colorA=18181B&colorB=FF7AC3
[hyperdata-version-href]: https://npmjs.com/package/@kodadot1/hyperdata

[if-that-version-src]: https://img.shields.io/npm/v/@kodadot1/if-that/latest.svg?style=flat&colorA=18181B&colorB=FF7AC3
[if-that-version-href]: https://npmjs.com/package/@kodadot1/if-that

Expand Down
15 changes: 15 additions & 0 deletions hyperdata/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.js]
indent_style = space
indent_size = 2

[{package.json,*.yml,*.cjson}]
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions hyperdata/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
dist
4 changes: 4 additions & 0 deletions hyperdata/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["plugin:vue/recommended", "prettier"],
"rules": {}
}
10 changes: 10 additions & 0 deletions hyperdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
coverage
dist
types
.vscode
.DS_Store
.eslintcache
*.log*
*.conf*
*.env*
5 changes: 5 additions & 0 deletions hyperdata/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "es5",
"semi": false
}
21 changes: 21 additions & 0 deletions hyperdata/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c)

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.
73 changes: 73 additions & 0 deletions hyperdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# @kodadot1/content

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]

> Magic static void main

## Usage

Install package:

```sh
# npm
npm install @kodadot1/content

# yarn
yarn add @kodadot1/content

# pnpm
pnpm install @kodadot1/content
```

Import:

```js
// ESM
import * as static from "@kodadot1/content";

// CommonJS
const static = require("@kodadot1/content");
```

## Available files

### 🔧 normalize

Unifying data structure into one format

- attributeFrom - unify attribute to one format
- contentFrom - unify metadata to one format
- normalize - sanitize content fields to one format

### 🔧 utils

Misc utils for manipulating with data

### 🔧 types

Misc types for metadata namely:

- OpenSea
- FxHash
- Tezos (TZIP-16)
- ERC-5773

## License

Made with 💛

Published under [MIT License](./LICENSE).

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/@kodadot1/content?style=flat-square
[npm-version-href]: https://npmjs.com/package/@kodadot1/content
[npm-downloads-src]: https://img.shields.io/npm/dm/@kodadot1/content?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/@kodadot1/content
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/@kodadot1/content/ci.yml?branch=main&style=flat-square
[github-actions-href]: https://github.com/@kodadot1/content/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/@kodadot1/content/main?style=flat-square
[codecov-href]: https://codecov.io/gh/@kodadot1/content
11 changes: 11 additions & 0 deletions hyperdata/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
build:
pnpm build

test:
pnpm test

publish:
pnpm publish --access public

c VERSION:
git commit -am ":bookmark: static@{{VERSION}}"
42 changes: 42 additions & 0 deletions hyperdata/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@kodadot1/hyperdata",
"version": "0.0.1-rc.1",
"description": "Get unified NFT content",
"repository": "kodadot/nft-gallery",
"license": "MIT",
"sideEffects": false,
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "vitest dev",
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
"prepack": "pnpm run build",
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
"test": "vitest run"
},
"devDependencies": {
"@kodadot1/minipfs": "0.4.0-rc.0",
"@vitest/coverage-c8": "^0.33.0",
"changelogen": "^0.5.4",
"eslint": "^8.47.0",
"eslint-config-unjs": "^0.2.1",
"prettier": "^2.8.8",
"typescript": "^4.9.5",
"unbuild": "^1.2.1",
"vitest": "^0.34.1"
}
}
4 changes: 4 additions & 0 deletions hyperdata/src/constatnts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const LEWD = 'NSFW'
// based on https://www.regextester.com/110183
// list ipfs://bafkreiequ3mnfu2ytaixzdzolcpfwlc3vrj4544pzqg4tjvyth2zi5mhgq
export const MIME_TYPE = /\w{4,12}\/[-+.\w]{2,62}/
3 changes: 3 additions & 0 deletions hyperdata/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './normalize'
export * from './utils'
export * from './types'
128 changes: 128 additions & 0 deletions hyperdata/src/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { MIME_TYPE } from './constatnts'
import {
Attribute,
Content,
FXHashMetadata,
GenArt,
OpenSeaAttribute,
OpenSeaMetadata,
PluralAssetMetadata,
PluralAttribute,
SanitizerFunc,
TezosAttribute,
TezosMetadata,
} from './types'

export function attributeFrom(attr: OpenSeaAttribute): Attribute
export function attributeFrom(attr: TezosAttribute): Attribute
export function attributeFrom(attr: PluralAttribute): Attribute
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function attributeFrom(attr: any): Attribute {
const display: string = attr.display_type || attr.type || ''
const trait: string | undefined = attr.trait_type || attr.name || attr.label
const value: string = attr.value?.toString()

return {
display,
trait: trait?.toString() ?? '',
value,
}
}

export function contentFrom(meta: OpenSeaMetadata, eager?: boolean): Content
export function contentFrom(meta: FXHashMetadata, eager?: boolean): Content
export function contentFrom(meta: TezosMetadata, eager?: boolean): Content
export function contentFrom(meta: PluralAssetMetadata, eager?: boolean): Content
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function contentFrom(meta: any, eager?: boolean): Content {
const description = meta.description || ''
const thumbnail = meta.thumbnailUri || meta.thumbnail
const image = meta.image || meta.displayUri || thumbnail || meta.mediaUri
const animationUrl = meta.animation_url || meta.mediaUri || meta.artifactUri
const attributes = meta.attributes?.map(attributeFrom) || []
const name = meta.name
const type = meta.type
const externalUrl = meta.external_url || meta.youtube_url || meta.externalUri
const tags = Array.isArray(meta.tags) ? meta.tags : []
let generative: GenArt | undefined

if (eager) {
generative = generativeFrom(meta)
}

return {
description,
image,
animationUrl, // rename to media?
attributes,
name,
type: MIME_TYPE.test(type) ? type : '',
externalUrl,
tags,
thumbnail,
generative,
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function generativeFrom(meta: any): GenArt | undefined {
const uri = meta.generativeUri || meta.generatorUri

if (!uri) {
return undefined
}

const previewHash = meta.previewHash
const previewParam = meta.previewParam || 'fxhash'
const capture = meta.capture
const settings = meta.settings

return {
uri,
previewHash,
previewParam,
capture,
settings,
}
}

export function normalize(content: Content, sanitizer: SanitizerFunc): Content {
return {
...content,
image: sanitizer(content.image),
animationUrl: sanitizer(content.animationUrl),
thumbnail: sanitizer(content.thumbnail),
}
}

export function mergeAttributes(
attrs: OpenSeaAttribute[],
overrides: Attribute[]
): Attribute[]
export function mergeAttributes(
attrs: TezosAttribute[],
overrides: Attribute[]
): Attribute[]
export function mergeAttributes(
attrs: PluralAttribute[],
overrides: Attribute[]
): Attribute[]
// export function mergeAttributes(attrs: Attribute[], overrides: Attribute[]): Attribute[]
export function mergeAttributes(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attrs: any[],
overrides: Attribute[]
): Attribute[] {
const initial = new Map(
attrs.map(attributeFrom).map((a) => [a.trait, a.value])
)

for (const override of overrides) {
initial.set(override.trait, override.value)
}

return Array.from(initial.entries()).map(([trait, value]) => ({
trait,
value,
}))
}
Loading