Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

iframe-interface

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iframe-interface

create interface from iframe

  • 0.0.3
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

iframe-interface

version npm downloads MIT License

iframe-interface helps you to register and call api between iframe. Support async.

English | 中文

GetStarted

yarn add iframe-interface

Basic Usage

iframe-interface separate api into 3 stage: factory => interface => call. Which is inherited from axios-interface (Another helpful api tool widely used by our team).

import {createFactory} from 'iframe-interface';

const {setIframe, createCall} = createFactory({side: 'host'});

const Component = () => <iframe src="https://example.com" ref={setIframe} />;

// define api
const getUsersFromIframe = createCall<Params, Result>('getUsers');

// call
const result = await getUsers({companyId: '1', keyword: 'jack'});
  • guest side
import {useCallback, useEffect} from 'react';
import {createFactory} from 'iframe-interface';

const {registerCallHandler} = createFactory({side: 'guest'});

// register api at global
registerCallHandler('getUsers', handle);

// or register api at component
const Component = () => {
    const handle = useCallback(
        async (params: Params) => {
            const result = await something();
            return result;
        },
        [],
    )
    useEffect(
        () => {
            registerCallHandler('getUsers', handle);
        },
        [],
    );
    return null;
}

registerCallHandler at host side and createCall at guest side is also allowed. Put them at the opposite sides and it will work.

Further

Lifecycles like onResolve are not implemented yet since I think it may not be necessary. If you have some common issue and you want it be implemented in iframe-interface, please open an issue.

FAQs

Package last updated on 10 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc