Typescript
Crea un archivo adsgram.d.ts y agregalo a la carpeta types o a una carpeta existente de declaraciones de tipos.
Información:
Si usas nuestros paquetes npm especificos para frameworks (React o Vue), el soporte de TypeScript esta incluido y no se necesitan declaraciones de tipos manuales.
Tipado de la API de AdsGram
ts
export interface ShowPromiseResult {
done: boolean;
description: string;
state: 'load' | 'render' | 'playing' | 'destroy';
error: boolean;
}
type BannerType = 'RewardedVideo' | 'FullscreenMedia';
interface AdsgramInitParams {
blockId: string;
debug?: boolean;
debugBannerType?: BannerType;
}
type EventType =
| 'onReward'
| 'onComplete'
| 'onStart'
| 'onSkip'
| 'onBannerNotFound'
| 'onNonStopShow'
| 'onTooLongSession'
| 'onError';
type HandlerType = () => void;
export interface AdController {
show(): Promise<ShowPromiseResult>;
addEventListener(event: EventType, handler: HandlerType): void;
removeEventListener(event: EventType, handler: HandlerType): void;
destroy(): void;
}
declare global {
interface Window {
Adsgram?: {
init(params: AdsgramInitParams): AdController;
};
}
}Tipado para <adsgram-task> en proyectos react/preact
Agrega el siguiente codigo a adsgram.d.ts para describir los tipos del web component adsgram-task:
ts
import { DOMAttributes } from "react";
type CustomElement<T> = Partial<T & DOMAttributes<T> & { children: any }> &
LibraryManagedAttributes;
declare module "react/jsx-runtime" {
namespace JSX {
interface IntrinsicElements extends JSXInternal {
"adsgram-task": CustomElement<HTMLDivElement>;
}
}
}ts
import { JSXInternal, LibraryManagedAttributes } from 'preact/src/jsx';
import DOMAttributes = JSXInternal.DOMAttributes;
type CustomElement<T> = Partial<T & DOMAttributes<T> & { children: any }> &
LibraryManagedAttributes;
declare module "preact/jsx-runtime" {
namespace JSX {
interface IntrinsicElements extends JSXInternal {
"adsgram-task": CustomElement<HTMLDivElement>;
}
}
}No es necesario agregar estos tipos para vue o vanilla js.