Typescript
Create adsgram.d.ts file and add it to types folder or existing folder with types declarations.
INFO
If you're using our framework-specific npm packages (React or Vue), TypeScript support is included out of the box and no manual type declarations are needed.
AdsGram API Typing
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;
};
}
}Typing for the <adsgram-task> in react/preact projects
Add the following code to adsgram.d.ts to describe the types for using the 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;
}
}
}There is no need to add these types for vue or vanilla js.