Typescript
فایل adsgram.d.ts ایجاد کنید و آن را به پوشه types یا پوشه موجود با اعلانهای types اضافه کنید.
اطلاعات
اگر از بستههای npm خاص فریمورک ما (React یا Vue) استفاده میکنید، پشتیبانی TypeScript به صورت پیشفرض شامل میشود و نیازی به اعلانهای type دستی نیست.
تایپ API مربوط به 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;
};
}
}تایپ <adsgram-task> در پروژههای react/preact
کد زیر را به adsgram.d.ts اضافه کنید تا types مربوط به استفاده از کامپوننت وب 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;
}
}
}نیازی به اضافه کردن این types برای vue یا vanilla js نیست.