@opensea/seadn 1.0.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensea/seadn",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Javascript SDK to work with SeaDN",
5
5
  "license": "MIT",
6
6
  "author": "OpenSea Developers",
@@ -12,7 +12,8 @@
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",
@@ -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
+ };