Skip to content

Commit

Permalink
refactor: update dotlottie-js exports
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Nov 4, 2024
1 parent 3666858 commit 926de78
Show file tree
Hide file tree
Showing 30 changed files with 110 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-points-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dotlottie/dotlottie-js': patch
---

fix: export missing types
3 changes: 3 additions & 0 deletions packages/dotlottie-js/src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ export function makeDotLottie<T extends 'v1' | 'v2'>(
}

export * from './v1/browser';
export * from './v1/common';
export * from './v2/browser';
export * from './v2/common';
export * from './utils';
export * from './types';
3 changes: 3 additions & 0 deletions packages/dotlottie-js/src/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ export function makeDotLottie<T extends 'v1' | 'v2'>(
}

export * from './v1/node';
export * from './v1/common';
export * from './v2/node';
export * from './v2/common';
export * from './utils';
export * from './types';
23 changes: 23 additions & 0 deletions packages/dotlottie-js/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2024 Design Barn Inc.
*/

import type { Animation as AnimationType } from '@lottie-animation-community/lottie-types';
import type { ZipOptions } from 'fflate';

export interface GetAnimationOptions {
inlineAssets?: boolean;
}

export interface ConversionOptions {
zipOptions?: ZipOptions;
}

export type AnimationData = AnimationType;

export interface ExportOptions {
inlineAssets?: boolean;
}

export type ImageData = string | ArrayBuffer | Blob;
export type AudioData = string | ArrayBuffer | Blob;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, test, expect, vi } from 'vitest';

import BULL_DATA from '../../../__tests__/__fixtures__/image-asset-optimization/bull.json';
import animationData from '../../../__tests__/__fixtures__/simple/animation/animations/lottie1.json';
import type { AnimationOptions } from '../../index.browser';
import type { AnimationOptionsV1 as AnimationOptions } from '../../index.browser';
import { LottieAnimationV1 } from '../../index.browser';

test('throws an error if it receives an invalid id when constructed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import bigMergedDotLottie from '../../../__tests__/__fixtures__/simple/big-merge
import editedDotlottieAnimation from '../../../__tests__/__fixtures__/simple/edited-settings.lottie?arraybuffer';
import editedAnimationData from '../../../__tests__/__fixtures__/simple/edited-settings/animations/lottie01.json';
import editedManifest from '../../../__tests__/__fixtures__/simple/edited-settings/manifest.json';
import type { AnimationData, AnimationOptions, ManifestAnimationV1, ManifestV1 } from '../../index.browser';
import type { AnimationData } from '../../../types';
import type { AnimationOptionsV1 as AnimationOptions, ManifestAnimationV1, ManifestV1 } from '../../index.browser';
import { DotLottieV1 as DotLottie, LottieAnimationV1 as LottieAnimation, PlayMode } from '../../index.browser';

describe('setAuthor', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, test, expect, vi } from 'vitest';

import BULL_DATA from '../../../__tests__/__fixtures__/image-asset-optimization/bull.json';
import animationData from '../../../__tests__/__fixtures__/simple/animation/animations/lottie1.json';
import type { AnimationOptions } from '../../index.node';
import type { AnimationOptionsV1 as AnimationOptions } from '../../index.node';
import { LottieAnimationV1 } from '../../index.node';

test('throws an error if it receives an invalid id when constructed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import bigMergedDotLottie from '../../../__tests__/__fixtures__/simple/big-merge
import editedDotlottieAnimation from '../../../__tests__/__fixtures__/simple/edited-settings.lottie?arraybuffer';
import editedAnimationData from '../../../__tests__/__fixtures__/simple/edited-settings/animations/lottie01.json';
import editedManifest from '../../../__tests__/__fixtures__/simple/edited-settings/manifest.json';
import type { AnimationData, AnimationOptions, ManifestAnimationV1, ManifestV1 } from '../../index.node';
import type { AnimationData } from '../../../types';
import type { AnimationOptionsV1 as AnimationOptions, ManifestAnimationV1, ManifestV1 } from '../../index.node';
import { DotLottieV1 as DotLottie, LottieAnimationV1 as LottieAnimation, PlayMode } from '../../index.node';

describe('setAuthor', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/dotlottie-js/src/v1/browser/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import type { Animation as AnimationType } from '@lottie-animation-community/lottie-types';

import { DotLottieError, getExtensionTypeFromBase64, isAudioAsset } from '../../utils';
import type { AnimationOptions } from '../common';
import type { AnimationOptionsV1 } from '../common';
import { LottieAnimationCommonV1 } from '../common';

import { LottieAudioV1 } from './audio';
import { LottieImageV1 } from './image';

export class LottieAnimationV1 extends LottieAnimationCommonV1 {
public constructor(options: AnimationOptions) {
public constructor(options: AnimationOptionsV1) {
super(options);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dotlottie-js/src/v1/browser/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Copyright 2023 Design Barn Inc.
*/

import type { AudioOptions } from '../common';
import type { AudioOptionsV1 } from '../common';
import { LottieAudioCommonV1 } from '../common';

export class LottieAudioV1 extends LottieAudioCommonV1 {
public constructor(options: AudioOptions) {
public constructor(options: AudioOptionsV1) {
super(options);
}
}
9 changes: 5 additions & 4 deletions packages/dotlottie-js/src/v1/browser/dotlottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Animation as AnimationType } from '@lottie-animation-community/lot
import type { Zippable } from 'fflate';
import { strToU8, zip, strFromU8, unzip } from 'fflate';

import type { ConversionOptions } from '../../types';
import {
base64ToUint8Array,
DotLottieError,
Expand All @@ -16,7 +17,7 @@ import {
isAudioAsset,
} from '../../utils';
import { DotLottie } from '../../v2/browser';
import type { AnimationOptions, DotLottieV1Options, ConversionOptions } from '../common';
import type { AnimationOptionsV1, DotLottieV1Options } from '../common';
import { DotLottieCommonV1 } from '../common';
import type { ManifestV1 } from '../common/schemas/manifest';

Expand Down Expand Up @@ -69,7 +70,7 @@ export class DotLottieV1 extends DotLottieCommonV1 {
}
}

public override addAnimation(animationOptions: AnimationOptions): DotLottieV1 {
public override addAnimation(animationOptions: AnimationOptionsV1): DotLottieV1 {
const animation = new LottieAnimationV1(animationOptions);

if (this._animationsMap.get(animationOptions.id)) {
Expand All @@ -81,7 +82,7 @@ export class DotLottieV1 extends DotLottieCommonV1 {
return this;
}

public override async toBase64(options: ConversionOptions | undefined): Promise<string> {
public override async toBase64(options?: ConversionOptions): Promise<string> {
const data = await this.toArrayBuffer(options);

const uint8Array = new Uint8Array(data);
Expand All @@ -90,7 +91,7 @@ export class DotLottieV1 extends DotLottieCommonV1 {
return window.btoa(binaryString);
}

public override async download(fileName: string, options: ConversionOptions | undefined = undefined): Promise<void> {
public override async download(fileName: string, options?: ConversionOptions): Promise<void> {
const blob = await this.toBlob(options);

const dataURL = URL.createObjectURL(blob);
Expand Down
4 changes: 2 additions & 2 deletions packages/dotlottie-js/src/v1/browser/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Copyright 2023 Design Barn Inc.
*/

import type { ImageOptions } from '../common';
import type { ImageOptionsV1 } from '../common';
import { LottieImageCommonV1 } from '../common';

export class LottieImageV1 extends LottieImageCommonV1 {
public constructor(options: ImageOptions) {
public constructor(options: ImageOptionsV1) {
super(options);
}
}
26 changes: 10 additions & 16 deletions packages/dotlottie-js/src/v1/common/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@
* Copyright 2023 Design Barn Inc.
*/

import type { Animation as AnimationType } from '@lottie-animation-community/lottie-types';
import type { ZipOptions } from 'fflate';

import type { AnimationData, ExportOptions } from '../../types';
import { DotLottieError, isAudioAsset } from '../../utils';

import type { LottieAudioCommonV1 } from './audio';
import type { LottieImageCommonV1 } from './image';
import { PlayMode } from './schemas/manifest';
import type { ManifestAnimationV1 } from './schemas/manifest';

export type AnimationData = AnimationType;

export interface ExportOptions {
inlineAssets?: boolean;
}

export interface AnimationOptionsBase extends ManifestAnimationV1 {
interface AnimationOptionsBase extends ManifestAnimationV1 {
defaultActiveAnimation?: boolean;
zipOptions?: ZipOptions;
}

export interface AnimationOptionsWithData extends AnimationOptionsBase {
interface AnimationOptionsWithData extends AnimationOptionsBase {
data: AnimationData;
url?: never;
}

export interface AnimationOptionsWithUrl extends AnimationOptionsBase {
interface AnimationOptionsWithUrl extends AnimationOptionsBase {
data?: never;
url: string;
}

export type AnimationOptions = AnimationOptionsWithData | AnimationOptionsWithUrl;
export type AnimationOptionsV1 = AnimationOptionsWithData | AnimationOptionsWithUrl;

export class LottieAnimationCommonV1 {
protected _data?: AnimationData;
Expand Down Expand Up @@ -68,7 +62,7 @@ export class LottieAnimationCommonV1 {

protected _audioAssets: LottieAudioCommonV1[] = [];

public constructor(options: AnimationOptions) {
public constructor(options: AnimationOptionsV1) {
this._requireValidOptions(options);

this._id = options.id;
Expand Down Expand Up @@ -312,7 +306,7 @@ export class LottieAnimationCommonV1 {
* @throws Error - if the animation data is not a valid Lottie animation data object.
* @throws Error - if the fetch request fails.
*/
public async toJSON(options?: ExportOptions): Promise<AnimationType> {
public async toJSON(options?: ExportOptions): Promise<AnimationData> {
if (this._url && !this._data) {
this._data = await this._fromUrl(this._url);
}
Expand All @@ -325,7 +319,7 @@ export class LottieAnimationCommonV1 {
await this._extractAudioAssets();

if (options?.inlineAssets) {
const animationAssets = this.data?.assets as AnimationType['assets'];
const animationAssets = this.data?.assets as AnimationData['assets'];

if (!animationAssets)
throw new DotLottieError("Failed to inline assets, the animation's assets are undefined.");
Expand Down Expand Up @@ -368,7 +362,7 @@ export class LottieAnimationCommonV1 {
* @throws Error - if the fetch request fails.
* @throws Error - if the data object is not a valid Lottie animation data object.
*/
private async _fromUrl(url: string): Promise<AnimationType> {
private async _fromUrl(url: string): Promise<AnimationData> {
const response = await fetch(url);

const text = await response.text();
Expand Down Expand Up @@ -479,7 +473,7 @@ export class LottieAnimationCommonV1 {
* @throws Error - if the url is not a valid url string.
* @throws Error - if the data object is not set and the url is not provided.
*/
private _requireValidOptions(options: AnimationOptions): asserts options is AnimationOptions {
private _requireValidOptions(options: AnimationOptionsV1): asserts options is AnimationOptionsV1 {
this._requireValidId(options.id);

if (!options.data && !options.url) {
Expand Down
7 changes: 3 additions & 4 deletions packages/dotlottie-js/src/v1/common/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import type { ZipOptions } from 'fflate';

import type { AudioData } from '../../types';
import { dataUrlFromU8, DotLottieError, ErrorCodes } from '../../utils';

import type { LottieAnimationCommonV1 } from './animation';

export type AudioData = string | ArrayBuffer | Blob;

export interface AudioOptions {
export interface AudioOptionsV1 {
data?: AudioData;
fileName: string;
id: string;
Expand All @@ -32,7 +31,7 @@ export class LottieAudioCommonV1 {

protected _zipOptions: ZipOptions;

public constructor(options: AudioOptions) {
public constructor(options: AudioOptionsV1) {
this._requireValidId(options.id);
this._requireValidFileName(options.fileName);

Expand Down
25 changes: 5 additions & 20 deletions packages/dotlottie-js/src/v1/common/dotlottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@
*/

import type { Animation as AnimationType } from '@lottie-animation-community/lottie-types';
import type { ZipOptions } from 'fflate';

import { PACKAGE_NAME } from '../../constants';
import type { ConversionOptions, GetAnimationOptions } from '../../types';
import { DotLottieError, isAudioAsset, isImageAsset, isValidURL } from '../../utils';

import type { AnimationOptions, LottieAnimationCommonV1 } from './animation';
import type { AnimationOptionsV1, LottieAnimationCommonV1 } from './animation';
import type { LottieAudioCommonV1 } from './audio';
import type { LottieImageCommonV1 } from './image';
import type { DotLottieV1Plugin } from './plugin';
import type { ManifestV1 } from './schemas/manifest';

export interface DotLottieV1Options {
author?: string;
customData?: Record<string, string>;
description?: string;
enableDuplicateImageOptimization?: boolean;
generator?: string;
keywords?: string;
plugins?: DotLottieV1Plugin[];
revision?: number;
version?: string;
}

export interface GetAnimationOptions {
inlineAssets?: boolean;
}

export interface ConversionOptions {
zipOptions?: ZipOptions;
}

export class DotLottieCommonV1 {
Expand Down Expand Up @@ -73,10 +62,6 @@ export class DotLottieCommonV1 {
this._keywords = options.keywords;
}

if (typeof options?.customData === 'object') {
this._customData = options.customData;
}

if (typeof options?.revision === 'number') {
this._revision = options.revision;
}
Expand All @@ -100,7 +85,7 @@ export class DotLottieCommonV1 {
throw new DotLottieError('addPlugins(...plugins: DotLottieV1Plugin[]) not implemented in concrete class!');
}

public addAnimation(_animationOptions: AnimationOptions): DotLottieCommonV1 {
public addAnimation(_animationOptions: AnimationOptionsV1): DotLottieCommonV1 {
throw new DotLottieError('addAnimation(animationOptions: AnimationOptions) not implemented in concrete class!');
}

Expand Down Expand Up @@ -362,9 +347,9 @@ export class DotLottieCommonV1 {
*/
public async getAnimation(
animationId: string,
options: GetAnimationOptions = {},
options?: GetAnimationOptions,
): Promise<LottieAnimationCommonV1 | undefined> {
if (!options.inlineAssets) return this._animationsMap.get(animationId);
if (!options?.inlineAssets) return this._animationsMap.get(animationId);

let dataWithInlinedImages = this._animationsMap.get(animationId);

Expand Down
7 changes: 3 additions & 4 deletions packages/dotlottie-js/src/v1/common/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import type { ZipOptions } from 'fflate';

import type { ImageData } from '../../types';
import { dataUrlFromU8, DotLottieError } from '../../utils';

import type { LottieAnimationCommonV1 } from './animation';

export type ImageData = string | ArrayBuffer | Blob;

export interface ImageOptions {
export interface ImageOptionsV1 {
data?: ImageData;
fileName: string;
id: string;
Expand All @@ -29,7 +28,7 @@ export class LottieImageCommonV1 {

protected _zipOptions: ZipOptions;

public constructor(options: ImageOptions) {
public constructor(options: ImageOptionsV1) {
this._requireValidId(options.id);
this._requireValidFileName(options.fileName);

Expand Down
Loading

0 comments on commit 926de78

Please sign in to comment.