Skip to content

Typescript ​

Crie um arquivo adsgram.d.ts e adicione-o à pasta types ou a uma pasta existente de declarações de tipos.

Informação:

Se você usa nossos pacotes npm específicos para frameworks (React ou Vue), o suporte a TypeScript está incluído e não são necessárias declarações de tipos manuais.

Tipagem da API do 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;
  debugConsole?: 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;
      };
    }
}

Tipagem para <adsgram-task> em projetos react/preact ​

Adicione o seguinte código a adsgram.d.ts para descrever os tipos do web component adsgram-task:

ts
import type { DetailedHTMLProps, HTMLAttributes } from "react";

type AdsgramTaskAttributes = DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> & {
  "data-block-id": string;
  "data-debug"?: boolean | "true" | "false";
  "data-debug-console"?: boolean | "true" | "false";
  classname?: string;
};

declare module "react" {
  namespace JSX {
    interface IntrinsicElements {
      "adsgram-task": AdsgramTaskAttributes;
    }
  }
}
ts
import type { JSX } from "preact";

type AdsgramTaskAttributes = JSX.HTMLAttributes<HTMLElement> & {
  "data-block-id": string;
  "data-debug"?: boolean | "true" | "false";
  "data-debug-console"?: boolean | "true" | "false";
  classname?: string;
};

declare module "preact" {
  namespace JSX {
    interface IntrinsicElements {
      "adsgram-task": AdsgramTaskAttributes;
    }
  }
}

Não é necessário adicionar estes tipos para vue ou vanilla js.