@opensea/seadn 1.0.6 → 1.0.7

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 CHANGED
@@ -8,8 +8,50 @@ npm i @opensea/seadn
8
8
 
9
9
  ## Usage
10
10
 
11
+ **Resizing**
12
+
11
13
  ```js
12
14
  import { resizeImage } from '@opensea/seadn';
13
15
 
14
16
  const resized = resizeImage(image, { height: 100, width: 100 });
15
17
  ```
18
+
19
+ **Usage with next/image**
20
+
21
+ ```jsx
22
+ import { isSupportedURL, resizeImage } from '@opensea/seadn';
23
+ import type { ImageLoader, ImageProps } from 'next/image';
24
+
25
+ // custom loader
26
+ export const seadnLoader: ImageLoader = ({ src, width }) => {
27
+ if (!isSupportedURL(src)) {
28
+ return src;
29
+ }
30
+ return resizeImage(src, { width });
31
+ };
32
+
33
+ // prop overrides
34
+ export const nextImageProps = <T extends ImageProps>({
35
+ src,
36
+ ...rest
37
+ }: T): ImageProps => {
38
+ if (isSupportedURL(src)) {
39
+ return {
40
+ ...rest,
41
+ src: resizeImage(src as string, {
42
+ width: rest.width,
43
+ height: rest.height
44
+ }),
45
+ unoptimized: true
46
+ };
47
+ }
48
+ return { src, ...rest };
49
+ };
50
+
51
+ // MyComponent.tsx
52
+ return (
53
+ <>
54
+ <Image {...nextImageProps({ alt: 'House', height: 200, src: imageUrl, width: 200 })} />
55
+ </>
56
+ )
57
+ ```
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- exports.isSupportedFormat=function(t){"string"==typeof t&&(t=new URL(t));var e=t.pathname;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")},exports.resizeImage=function(t,e){var r=e.width,i=e.height,n=e.dpr,s=void 0===n?1:n,o=e.freezeAnimation,a=void 0!==o&&o;"string"==typeof t&&(t=new URL(t));var g=new URLSearchParams({auto:"format",dpr:String(s)});return void 0!==r&&g.set("w",String(r)),void 0!==i&&g.set("h",String(i)),a&&g.set("fr","1"),t.search=g.toString(),t.toString()};
1
+ var t=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;var r=t.pathname;return r.endsWith(".jpg")||r.endsWith(".jpeg")||r.endsWith(".png")||r.endsWith(".webp")||r.endsWith(".gif")||r.startsWith("/gae/")};exports.isSupportedFormat=t,exports.isSupportedURL=function(r){try{"string"==typeof r&&(r=new URL(r))}catch(t){return!1}return r instanceof URL&&"i.seadn.io"===r.hostname&&t(r)},exports.resizeImage=function(t,r){var e=r.width,n=r.height,i=r.dpr,o=void 0===i?1:i,s=r.freezeAnimation,a=void 0!==s&&s;try{"string"==typeof t&&(t=new URL(t))}catch(r){return t.toString()}if(!(t instanceof URL))return t;var h=new URLSearchParams({auto:"format",dpr:String(o)});return void 0!==e&&h.set("w",String(e)),void 0!==n&&h.set("h",String(n)),a&&h.set("fr","1"),t.search=h.toString(),t.toString()};
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["image","URL","pathname","endsWith","startsWith","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","params","URLSearchParams","auto","String","undefined","set","search","toString"],"mappings":"0BAmCiC,SAACA,GACX,iBAAVA,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAQE,EAAaF,EAAbE,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,sBA1C2B,SACzBJ,EAAmBK,GAET,IADRC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAE5B,iBAAVV,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAMY,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNL,IAAKM,OAAON,KAed,YAZcO,IAAVV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,SAEVU,IAAXT,GACFK,EAAOK,IAAI,IAAKF,OAAOR,IAGrBI,GACFC,EAAOK,IAAI,KAAM,KAGnBjB,EAAMkB,OAASN,EAAOO,WACfnB,EAAMmB,UACf"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n try {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n } catch {\n return image.toString();\n }\n\n if (!(image instanceof URL)) {\n return image;\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["isSupportedFormat","src","URL","_unused2","pathname","endsWith","startsWith","_unused3","hostname","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search"],"mappings":"AAOa,IAoCAA,EAAoB,SAACC,GAChC,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAE,GACA,OACD,CAAA,CAED,KAAMF,aAAeC,KACnB,OAAO,EAGT,IAAQE,EAAaH,EAAbG,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,qDAE8B,SAACL,GAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAM,GACA,OACD,CAAA,CACD,OAAMN,aAAeC,KAGG,eAAjBD,EAAIO,UAA6BR,EAAkBC,EAC5D,sBAxE2B,SACzBQ,EAAmBC,GACjB,IAAAC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAEjD,IACuB,iBAAVN,IACTA,EAAQ,IAAIP,IAAIO,GAEnB,CAAC,MAAAQ,GACA,OAAOR,EAAMS,UACd,CAED,KAAMT,aAAiBP,KACrB,OAAOO,EAGT,IAAMU,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNP,IAAKQ,OAAOR,KAed,YAZcS,IAAVZ,GACFQ,EAAOK,IAAI,IAAKF,OAAOX,SAEVY,IAAXX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,IAGrBI,GACFG,EAAOK,IAAI,KAAM,KAGnBf,EAAMgB,OAASN,EAAOD,WACfT,EAAMS,UACf"}
package/dist/index.d.ts CHANGED
@@ -2,9 +2,10 @@
2
2
  export type ResizeParams = {
3
3
  freezeAnimation?: boolean;
4
4
  dpr?: number;
5
- width?: number;
6
- height?: number;
5
+ width?: number | `${number}`;
6
+ height?: number | `${number}`;
7
7
  };
8
8
  export declare const resizeImage: (image: string | URL, { width, height, dpr, freezeAnimation }: ResizeParams) => string;
9
- export declare const isSupportedFormat: (image: string | URL) => boolean;
9
+ export declare const isSupportedFormat: (src: string | URL) => boolean;
10
+ export declare const isSupportedURL: (src: string | URL) => boolean;
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,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,2CACkC,YAAY,KAChE,MAuBF,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,MAAM,GAAG,GAAG,YAcpD,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;CAC/B,CAAC;AAEF,eAAO,MAAM,WAAW,UACf,MAAM,GAAG,GAAG,2CACkC,YAAY,KAChE,MA+BF,CAAC;AAEF,eAAO,MAAM,iBAAiB,QAAS,MAAM,GAAG,GAAG,KAAG,OAsBrD,CAAC;AAEF,eAAO,MAAM,cAAc,QAAS,MAAM,GAAG,GAAG,KAAG,OAYlD,CAAC"}
@@ -1,2 +1,2 @@
1
- const t=(t,{width:e,height:n,dpr:r=1,freezeAnimation:i=!1})=>{"string"==typeof t&&(t=new URL(t));const s=new URLSearchParams({auto:"format",dpr:String(r)});return void 0!==e&&s.set("w",String(e)),void 0!==n&&s.set("h",String(n)),i&&s.set("fr","1"),t.search=s.toString(),t.toString()},e=t=>{"string"==typeof t&&(t=new URL(t));const{pathname:e}=t;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")};export{e as isSupportedFormat,t as resizeImage};
1
+ const t=(t,{width:n,height:e,dpr:r=1,freezeAnimation:i=!1})=>{try{"string"==typeof t&&(t=new URL(t))}catch(n){return t.toString()}if(!(t instanceof URL))return t;const s=new URLSearchParams({auto:"format",dpr:String(r)});return void 0!==n&&s.set("w",String(n)),void 0!==e&&s.set("h",String(e)),i&&s.set("fr","1"),t.search=s.toString(),t.toString()},n=t=>{try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;const{pathname:n}=t;return n.endsWith(".jpg")||n.endsWith(".jpeg")||n.endsWith(".png")||n.endsWith(".webp")||n.endsWith(".gif")||n.startsWith("/gae/")},e=t=>{try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}return t instanceof URL&&"i.seadn.io"===t.hostname&&n(t)};export{n as isSupportedFormat,e as isSupportedURL,t as resizeImage};
2
2
  //# sourceMappingURL=index.modern.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["resizeImage","image","width","height","dpr","freezeAnimation","URL","params","URLSearchParams","auto","String","undefined","set","search","toString","isSupportedFormat","pathname","endsWith","startsWith"],"mappings":"AAOa,MAAAA,EAAcA,CACzBC,GACEC,QAAOC,SAAQC,IAAAA,EAAM,EAAGC,gBAAAA,GAAkB,MAEvB,iBAAVJ,IACTA,EAAQ,IAAIK,IAAIL,IAGlB,MAAMM,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNL,IAAKM,OAAON,KAed,YAZcO,IAAVT,GACFK,EAAOK,IAAI,IAAKF,OAAOR,SAEVS,IAAXR,GACFI,EAAOK,IAAI,IAAKF,OAAOP,IAGrBE,GACFE,EAAOK,IAAI,KAAM,KAGnBX,EAAMY,OAASN,EAAOO,WACfb,EAAMa,UACf,EAEaC,EAAqBd,IACX,iBAAVA,IACTA,EAAQ,IAAIK,IAAIL,IAGlB,MAAMe,SAAEA,GAAaf,EACrB,OACEe,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAAO"}
1
+ {"version":3,"file":"index.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n try {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n } catch {\n return image.toString();\n }\n\n if (!(image instanceof URL)) {\n return image;\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["resizeImage","image","width","height","dpr","freezeAnimation","URL","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search","isSupportedFormat","src","_unused2","pathname","endsWith","startsWith","isSupportedURL","_unused3","hostname"],"mappings":"AAOa,MAAAA,EAAcA,CACzBC,GACEC,QAAOC,SAAQC,IAAAA,EAAM,EAAGC,gBAAAA,GAAkB,MAE5C,IACuB,iBAAVJ,IACTA,EAAQ,IAAIK,IAAIL,GAEnB,CAAC,MAAAM,GACA,OAAON,EAAMO,UACd,CAED,KAAMP,aAAiBK,KACrB,OAAOL,EAGT,MAAMQ,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNP,IAAKQ,OAAOR,KAed,YAZcS,IAAVX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,SAEVW,IAAXV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,IAGrBE,GACFI,EAAOK,IAAI,KAAM,KAGnBb,EAAMc,OAASN,EAAOD,WACfP,EAAMO,UACf,EAEaQ,EAAqBC,IAChC,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAC,GACA,OACD,CAAA,CAED,KAAMD,aAAeX,KACnB,OACD,EAED,MAAMa,SAAEA,GAAaF,EACrB,OACEE,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAAO,EAIlBC,EAAkBL,IAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAM,GACA,QACD,CACD,OAAMN,aAAeX,KAGG,eAAjBW,EAAIO,UAA6BR,EAAkBC,EAAG"}
@@ -1,2 +1,2 @@
1
- var t=function(t,e){var n=e.width,r=e.height,i=e.dpr,a=void 0===i?1:i,o=e.freezeAnimation,s=void 0!==o&&o;"string"==typeof t&&(t=new URL(t));var g=new URLSearchParams({auto:"format",dpr:String(a)});return void 0!==n&&g.set("w",String(n)),void 0!==r&&g.set("h",String(r)),s&&g.set("fr","1"),t.search=g.toString(),t.toString()},e=function(t){"string"==typeof t&&(t=new URL(t));var e=t.pathname;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")};export{e as isSupportedFormat,t as resizeImage};
1
+ var t=function(t,n){var r=n.width,e=n.height,i=n.dpr,a=void 0===i?1:i,o=n.freezeAnimation,s=void 0!==o&&o;try{"string"==typeof t&&(t=new URL(t))}catch(n){return t.toString()}if(!(t instanceof URL))return t;var h=new URLSearchParams({auto:"format",dpr:String(a)});return void 0!==r&&h.set("w",String(r)),void 0!==e&&h.set("h",String(e)),s&&h.set("fr","1"),t.search=h.toString(),t.toString()},n=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;var n=t.pathname;return n.endsWith(".jpg")||n.endsWith(".jpeg")||n.endsWith(".png")||n.endsWith(".webp")||n.endsWith(".gif")||n.startsWith("/gae/")},r=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}return t instanceof URL&&"i.seadn.io"===t.hostname&&n(t)};export{n as isSupportedFormat,r as isSupportedURL,t as resizeImage};
2
2
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.module.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["resizeImage","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","URL","params","URLSearchParams","auto","String","undefined","set","search","toString","isSupportedFormat","pathname","endsWith","startsWith"],"mappings":"AAOa,IAAAA,EAAc,SACzBC,EAAmBC,GAET,IADRC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAE5B,iBAAVN,IACTA,EAAQ,IAAIQ,IAAIR,IAGlB,IAAMS,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNN,IAAKO,OAAOP,KAed,YAZcQ,IAAVX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,SAEVW,IAAXV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,IAGrBI,GACFE,EAAOK,IAAI,KAAM,KAGnBd,EAAMe,OAASN,EAAOO,WACfhB,EAAMgB,UACf,EAEaC,EAAoB,SAACjB,GACX,iBAAVA,IACTA,EAAQ,IAAIQ,IAAIR,IAGlB,IAAQkB,EAAalB,EAAbkB,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB"}
1
+ {"version":3,"file":"index.module.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n try {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n } catch {\n return image.toString();\n }\n\n if (!(image instanceof URL)) {\n return image;\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["resizeImage","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","URL","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search","isSupportedFormat","src","_unused2","pathname","endsWith","startsWith","isSupportedURL","_unused3","hostname"],"mappings":"AAOa,IAAAA,EAAc,SACzBC,EAAmBC,GACjB,IAAAC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAEjD,IACuB,iBAAVN,IACTA,EAAQ,IAAIQ,IAAIR,GAEnB,CAAC,MAAAS,GACA,OAAOT,EAAMU,UACd,CAED,KAAMV,aAAiBQ,KACrB,OAAOR,EAGT,IAAMW,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNR,IAAKS,OAAOT,KAed,YAZcU,IAAVb,GACFS,EAAOK,IAAI,IAAKF,OAAOZ,SAEVa,IAAXZ,GACFQ,EAAOK,IAAI,IAAKF,OAAOX,IAGrBI,GACFI,EAAOK,IAAI,KAAM,KAGnBhB,EAAMiB,OAASN,EAAOD,WACfV,EAAMU,UACf,EAEaQ,EAAoB,SAACC,GAChC,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAC,GACA,OACD,CAAA,CAED,KAAMD,aAAeX,KACnB,OAAO,EAGT,IAAQa,EAAaF,EAAbE,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,EAEaC,EAAiB,SAACL,GAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAM,GACA,OACD,CAAA,CACD,OAAMN,aAAeX,KAGG,eAAjBW,EAAIO,UAA6BR,EAAkBC,EAC5D"}
package/dist/index.umd.js CHANGED
@@ -1,2 +1,2 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).seadn={})}(this,function(e){e.isSupportedFormat=function(e){"string"==typeof e&&(e=new URL(e));var t=e.pathname;return t.endsWith(".jpg")||t.endsWith(".jpeg")||t.endsWith(".png")||t.endsWith(".webp")||t.endsWith(".gif")||t.startsWith("/gae/")},e.resizeImage=function(e,t){var n=t.width,i=t.height,o=t.dpr,r=void 0===o?1:o,s=t.freezeAnimation,d=void 0!==s&&s;"string"==typeof e&&(e=new URL(e));var f=new URLSearchParams({auto:"format",dpr:String(r)});return void 0!==n&&f.set("w",String(n)),void 0!==i&&f.set("h",String(i)),d&&f.set("fr","1"),e.search=f.toString(),e.toString()}});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).seadn={})}(this,function(t){var e=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;var e=t.pathname;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")};t.isSupportedFormat=e,t.isSupportedURL=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}return t instanceof URL&&"i.seadn.io"===t.hostname&&e(t)},t.resizeImage=function(t,e){var n=e.width,i=e.height,r=e.dpr,o=void 0===r?1:r,s=e.freezeAnimation,a=void 0!==s&&s;try{"string"==typeof t&&(t=new URL(t))}catch(e){return t.toString()}if(!(t instanceof URL))return t;var f=new URLSearchParams({auto:"format",dpr:String(o)});return void 0!==n&&f.set("w",String(n)),void 0!==i&&f.set("h",String(i)),a&&f.set("fr","1"),t.search=f.toString(),t.toString()}});
2
2
  //# sourceMappingURL=index.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["image","URL","pathname","endsWith","startsWith","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","params","URLSearchParams","auto","String","undefined","set","search","toString"],"mappings":"mPAmCiC,SAACA,GACX,iBAAVA,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAQE,EAAaF,EAAbE,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,gBA1C2B,SACzBJ,EAAmBK,GAET,IADRC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAE5B,iBAAVV,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAMY,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNL,IAAKM,OAAON,KAed,YAZcO,IAAVV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,SAEVU,IAAXT,GACFK,EAAOK,IAAI,IAAKF,OAAOR,IAGrBI,GACFC,EAAOK,IAAI,KAAM,KAGnBjB,EAAMkB,OAASN,EAAOO,WACfnB,EAAMmB,UACf"}
1
+ {"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n try {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n } catch {\n return image.toString();\n }\n\n if (!(image instanceof URL)) {\n return image;\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["isSupportedFormat","src","URL","_unused2","pathname","endsWith","startsWith","_unused3","hostname","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search"],"mappings":"+NAOa,IAoCAA,EAAoB,SAACC,GAChC,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAE,GACA,OACD,CAAA,CAED,KAAMF,aAAeC,KACnB,OAAO,EAGT,IAAQE,EAAaH,EAAbG,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,yCAE8B,SAACL,GAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAM,GACA,OACD,CAAA,CACD,OAAMN,aAAeC,KAGG,eAAjBD,EAAIO,UAA6BR,EAAkBC,EAC5D,gBAxE2B,SACzBQ,EAAmBC,GACjB,IAAAC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAEjD,IACuB,iBAAVN,IACTA,EAAQ,IAAIP,IAAIO,GAEnB,CAAC,MAAAQ,GACA,OAAOR,EAAMS,UACd,CAED,KAAMT,aAAiBP,KACrB,OAAOO,EAGT,IAAMU,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNP,IAAKQ,OAAOR,KAed,YAZcS,IAAVZ,GACFQ,EAAOK,IAAI,IAAKF,OAAOX,SAEVY,IAAXX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,IAGrBI,GACFG,EAAOK,IAAI,KAAM,KAGnBf,EAAMgB,OAASN,EAAOD,WACfT,EAAMS,UACf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensea/seadn",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Javascript SDK to work with SeaDN",
5
5
  "license": "MIT",
6
6
  "author": "OpenSea Developers",
@@ -28,15 +28,15 @@
28
28
  "sideEffects": false,
29
29
  "types": "./dist/index.d.ts",
30
30
  "devDependencies": {
31
- "@vitest/coverage-c8": "0.29.1",
32
- "concurrently": "7.6.0",
33
- "esbuild": "0.17.10",
31
+ "@vitest/coverage-v8": "0.33.0",
32
+ "concurrently": "8.2.0",
33
+ "esbuild": "0.19.0",
34
34
  "husky": "8.0.3",
35
- "lint-staged": "13.1.2",
35
+ "lint-staged": "13.2.3",
36
36
  "microbundle": "0.15.1",
37
- "prettier": "2.8.4",
37
+ "prettier": "3.0.1",
38
38
  "prettier-package-json": "2.8.0",
39
- "vitest": "0.29.1"
39
+ "vitest": "0.33.0"
40
40
  },
41
41
  "lint-staged": {
42
42
  "package.json": [
package/src/index.test.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { expect, test } from 'vitest';
2
- import { resizeImage, isSupportedFormat } from '.';
2
+ import { resizeImage, isSupportedFormat, isSupportedURL } from '.';
3
3
 
4
4
  test.each([
5
5
  [
@@ -26,9 +26,29 @@ test.each([
26
26
  'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
27
27
  { dpr: 5, freezeAnimation: true },
28
28
  'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=5&fr=1'
29
- ]
30
- ])('resizeSeadnImage(%i, %i) -> %i', (url, resizeParams, expected) => {
31
- expect(resizeImage(url, resizeParams)).toBe(expected);
29
+ ],
30
+ ['unknown', { dpr: 5, freezeAnimation: true }, 'unknown'],
31
+ [5, { dpr: 5, freezeAnimation: true }, 5]
32
+ ])(
33
+ 'resizeImage(%url, %resizeParams) -> %expected',
34
+ (url, resizeParams, expected) => {
35
+ expect(resizeImage(url as string, resizeParams)).toBe(expected);
36
+ }
37
+ );
38
+
39
+ test.each([
40
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png', true],
41
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpg', true],
42
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpeg', true],
43
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
44
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
45
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.gif', true],
46
+ ['https://i.seadn.io/gae/files/057bb30c6d80d54b78439927b2f07676', true],
47
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false],
48
+ ['unknown', false],
49
+ [5, false]
50
+ ])('isSupportedFormat(%url) -> %expected', (url, expected) => {
51
+ expect(isSupportedFormat(url as string)).toBe(expected);
32
52
  });
33
53
 
34
54
  test.each([
@@ -39,7 +59,10 @@ test.each([
39
59
  ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
40
60
  ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.gif', true],
41
61
  ['https://i.seadn.io/gae/files/057bb30c6d80d54b78439927b2f07676', true],
42
- ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false]
43
- ])('isSupportedFormat(%i) -> %i', (url, expected) => {
44
- expect(isSupportedFormat(url)).toBe(expected);
62
+ ['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false],
63
+ ['https://i.seadn2.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false],
64
+ ['unknown', false],
65
+ [5, false]
66
+ ])('isSeadnUrl(%url) -> %expected', (url, expected) => {
67
+ expect(isSupportedURL(url as string)).toBe(expected);
45
68
  });
package/src/index.ts CHANGED
@@ -1,16 +1,24 @@
1
1
  export type ResizeParams = {
2
2
  freezeAnimation?: boolean;
3
3
  dpr?: number;
4
- width?: number;
5
- height?: number;
4
+ width?: number | `${number}`;
5
+ height?: number | `${number}`;
6
6
  };
7
7
 
8
8
  export const resizeImage = (
9
9
  image: string | URL,
10
10
  { width, height, dpr = 1, freezeAnimation = false }: ResizeParams
11
11
  ): string => {
12
- if (typeof image === 'string') {
13
- image = new URL(image);
12
+ try {
13
+ if (typeof image === 'string') {
14
+ image = new URL(image);
15
+ }
16
+ } catch {
17
+ return image.toString();
18
+ }
19
+
20
+ if (!(image instanceof URL)) {
21
+ return image;
14
22
  }
15
23
 
16
24
  const params = new URLSearchParams({
@@ -33,12 +41,20 @@ export const resizeImage = (
33
41
  return image.toString();
34
42
  };
35
43
 
36
- export const isSupportedFormat = (image: string | URL) => {
37
- if (typeof image === 'string') {
38
- image = new URL(image);
44
+ export const isSupportedFormat = (src: string | URL): boolean => {
45
+ try {
46
+ if (typeof src === 'string') {
47
+ src = new URL(src);
48
+ }
49
+ } catch {
50
+ return false;
51
+ }
52
+
53
+ if (!(src instanceof URL)) {
54
+ return false;
39
55
  }
40
56
 
41
- const { pathname } = image;
57
+ const { pathname } = src;
42
58
  return (
43
59
  pathname.endsWith('.jpg') ||
44
60
  pathname.endsWith('.jpeg') ||
@@ -48,3 +64,17 @@ export const isSupportedFormat = (image: string | URL) => {
48
64
  pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions
49
65
  );
50
66
  };
67
+
68
+ export const isSupportedURL = (src: string | URL): boolean => {
69
+ try {
70
+ if (typeof src === 'string') {
71
+ src = new URL(src);
72
+ }
73
+ } catch {
74
+ return false;
75
+ }
76
+ if (!(src instanceof URL)) {
77
+ return false;
78
+ }
79
+ return src.hostname === 'i.seadn.io' && isSupportedFormat(src);
80
+ };