@opensea/seadn 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # SeaDN Javascript SDK
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ npm i @opensea/seadn
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```js
12
+ import { resizeImage } from '@opensea/seadn';
13
+
14
+ const resized = resizeImage(image, { height: 100, width: 100 });
15
+ ```
package/dist/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export type ResizeParams = {
6
6
  };
7
7
  export declare const resizeImage: (image: string | URL, { width, height, dpr }: ResizeParams) => string;
8
8
  export declare const isSupportedFormat: (image: string | URL) => boolean;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,WAAW,UACf,MAAM,GAAG,GAAG,0BACS,YAAY,KACvC,MAmBF,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,MAAM,GAAG,GAAG,YAcpD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensea/seadn",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Javascript SDK to work with SeaDN",
5
5
  "license": "MIT",
6
6
  "author": "OpenSea Developers",
@@ -12,20 +12,24 @@
12
12
  "main": "./dist/index.cjs",
13
13
  "module": "./dist/index.module.js",
14
14
  "files": [
15
- "dist"
15
+ "./dist",
16
+ "./src"
16
17
  ],
17
18
  "scripts": {
18
19
  "build": "microbundle",
19
20
  "check-types": "tsc --noEmit",
21
+ "lint": "concurrently \"npm run check-types\" \"npm run prettier:check\" \"npm run prettier:package.json:check\"",
20
22
  "prepare": "husky install",
21
23
  "prettier:check": "prettier --check .",
22
- "prettier:check:package.json": "prettier-package-json --list-different",
23
24
  "prettier:fix": "prettier --write .",
25
+ "prettier:package.json:check": "prettier-package-json --list-different",
24
26
  "test": "vitest"
25
27
  },
26
28
  "sideEffects": false,
27
29
  "types": "./dist/index.d.ts",
28
30
  "devDependencies": {
31
+ "@vitest/coverage-c8": "^0.29.1",
32
+ "concurrently": "^7.6.0",
29
33
  "esbuild": "^0.17.10",
30
34
  "husky": "^8.0.3",
31
35
  "lint-staged": "^13.1.2",
@@ -0,0 +1,40 @@
1
+ import { expect, test } from 'vitest';
2
+ import { resizeImage, isSupportedFormat } from '.';
3
+
4
+ test.each([
5
+ [
6
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
7
+ { height: 100 },
8
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&h=100'
9
+ ],
10
+ [
11
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
12
+ { width: 100 },
13
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100'
14
+ ],
15
+ [
16
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
17
+ { height: 100, width: 100 },
18
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100&h=100'
19
+ ],
20
+ [
21
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100&h=100',
22
+ { height: 200, width: 200 },
23
+ 'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=200&h=200'
24
+ ]
25
+ ])('resizeSeadnImage(%i, %i) -> %i', (url, resizeParams, expected) => {
26
+ expect(resizeImage(url, resizeParams)).toBe(expected);
27
+ });
28
+
29
+ test.each([
30
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png', true],
31
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpg', true],
32
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpeg', true],
33
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
34
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
35
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.gif', true],
36
+ ['https://i.seadn.io/gae/files/057bb30c6d80d54b78439927b2f07676', true],
37
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false]
38
+ ])('isSupportedFormat(%i) -> %i', (url, expected) => {
39
+ expect(isSupportedFormat(url)).toBe(expected);
40
+ });
package/src/index.ts ADDED
@@ -0,0 +1,45 @@
1
+ export type ResizeParams = {
2
+ dpr?: number;
3
+ width?: number;
4
+ height?: number;
5
+ };
6
+
7
+ export const resizeImage = (
8
+ image: string | URL,
9
+ { width, height, dpr = 1 }: ResizeParams
10
+ ): string => {
11
+ if (typeof image === 'string') {
12
+ image = new URL(image);
13
+ }
14
+
15
+ const params = new URLSearchParams({
16
+ auto: 'format',
17
+ dpr: String(dpr)
18
+ });
19
+
20
+ if (width !== undefined) {
21
+ params.set('w', String(width));
22
+ }
23
+ if (height !== undefined) {
24
+ params.set('h', String(height));
25
+ }
26
+
27
+ image.search = params.toString();
28
+ return image.toString();
29
+ };
30
+
31
+ export const isSupportedFormat = (image: string | URL) => {
32
+ if (typeof image === 'string') {
33
+ image = new URL(image);
34
+ }
35
+
36
+ const { pathname } = image;
37
+ return (
38
+ pathname.endsWith('.jpg') ||
39
+ pathname.endsWith('.jpeg') ||
40
+ pathname.endsWith('.png') ||
41
+ pathname.endsWith('.webp') ||
42
+ pathname.endsWith('.gif') ||
43
+ pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions
44
+ );
45
+ };