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

react-instagram-embed

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-instagram-embed - npm Package Compare versions

Comparing version 1.4.2 to 1.5.0

es/index.d.ts

419

es/index.js

@@ -1,258 +0,169 @@

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React, { Component } from 'react';
import qs from 'query-string';
var InstagramEmbed =
/*#__PURE__*/
function (_Component) {
_inherits(InstagramEmbed, _Component);
function InstagramEmbed() {
var _getPrototypeOf2;
var _this2;
_classCallCheck(this, InstagramEmbed);
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
_this2 = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(InstagramEmbed)).call.apply(_getPrototypeOf2, [this].concat(_args)));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "request", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "_timer", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "state", {
__html: null
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "handleFetchSuccess", function (response) {
_this2.props.onSuccess && _this2.props.onSuccess(response);
_this2.setState({
__html: response.html
}, function () {
window.instgrm.Embeds.process();
_this2.props.onAfterRender && _this2.props.onAfterRender();
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "handleFetchFailure", function () {
var _this2$props;
clearTimeout(_this2._timer);
_this2.props.onFailure && (_this2$props = _this2.props).onFailure.apply(_this2$props, arguments);
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "cancel", function () {
if (_this2.request) {
_this2.request.cancel();
}
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "createRequestPromise", function (url) {
for (var _len2 = arguments.length, opts = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
opts[_key2 - 1] = arguments[_key2];
}
var request = {};
request.promise = new Promise(function (resolve, reject) {
var promise = fetch(url, _objectSpread({}, opts)).then(function (response) {
return response.json();
}).then(function (json) {
return resolve(json);
}).catch(function (err) {
return reject(err);
});
request.cancel = function () {
return reject(new Error('Cancelled'));
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
import * as React from 'react';
import * as qs from 'query-string';
var InstagramEmbed = /** @class */ (function (_super) {
__extends(InstagramEmbed, _super);
function InstagramEmbed(props) {
var _this = _super.call(this, props) || this;
_this.request = null;
// Public
_this.cancel = function () {
if (_this.request) {
_this.request.cancel();
}
};
return promise;
});
return request;
});
return _this2;
}
_createClass(InstagramEmbed, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this3 = this;
if (window.instgrm) {
this.fetchEmbed(this.getQueryParams(this.props));
} else {
if (this.props.injectScript && !document.getElementById('react-instagram-embed-script')) {
this.injectScript();
}
this.checkAPI().then(function () {
return _this3.fetchEmbed(_this3.getQueryParams(_this3.props));
});
}
_this.handleFetchSuccess = function (response) {
if (_this.props.onSuccess) {
_this.props.onSuccess(response);
}
_this.setState({ html: response.html }, function () {
window.instgrm.Embeds.process();
if (_this.props.onAfterRender) {
_this.props.onAfterRender();
}
});
};
_this.handleFetchFailure = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
clearTimeout(_this.timer);
if (_this.props.onFailure) {
_this.props.onFailure(args);
}
};
_this.createRequestPromise = function (url) {
var request = {};
request.promise = new Promise(function (resolve, reject) {
var promise = fetch(url)
.then(function (response) { return response.json(); })
.then(function (json) { return resolve(json); })
.catch(function (err) { return reject(err); });
request.cancel = function () { return reject(new Error('Cancelled')); };
return promise;
});
return request;
};
_this.state = { html: null };
return _this;
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props = this.props,
url = _this$props.url,
hideCaption = _this$props.hideCaption,
maxWidth = _this$props.maxWidth,
containerTagName = _this$props.containerTagName;
if (prevProps.url !== url || prevProps.hideCaption !== hideCaption || prevProps.maxWidth !== maxWidth || prevProps.containerTagName !== containerTagName) {
this.request.cancel();
this.fetchEmbed(this.getQueryParams(this.props));
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps, nextState) {
var _this$props2 = this.props,
url = _this$props2.url,
hideCaption = _this$props2.hideCaption,
maxWidth = _this$props2.maxWidth,
containerTagName = _this$props2.containerTagName,
onLoading = _this$props2.onLoading,
onSuccess = _this$props2.onSuccess,
onAfterRender = _this$props2.onAfterRender,
onFailure = _this$props2.onFailure;
var __html = this.state.__html;
if (nextProps.url !== url || nextProps.hideCaption !== hideCaption || nextProps.maxWidth !== maxWidth || nextProps.containerTagName !== containerTagName || nextProps.onLoading !== onLoading || nextProps.onSuccess !== onSuccess || nextProps.onAfterRender !== onAfterRender || nextProps.onFailure !== onFailure || nextState.__html !== __html) {
return true;
}
return false;
}
}, {
key: "render",
value: function render() {
var Tag = this.props.containerTagName;
return React.createElement(Tag, _extends({}, this.omitComponentProps(), {
dangerouslySetInnerHTML: {
__html: this.state.__html
InstagramEmbed.prototype.componentDidMount = function () {
var _this = this;
if (window.instgrm) {
this.fetchEmbed(this.getQueryParams(this.props));
}
}));
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.cancel();
}
}, {
key: "omitComponentProps",
value: function omitComponentProps() {
// eslint-disable-next-line no-unused-vars
var _this$props3 = this.props,
url = _this$props3.url,
hideCaption = _this$props3.hideCaption,
maxWidth = _this$props3.maxWidth,
containerTagName = _this$props3.containerTagName,
onLoading = _this$props3.onLoading,
onSuccess = _this$props3.onSuccess,
onAfterRender = _this$props3.onAfterRender,
onFailure = _this$props3.onFailure,
protocol = _this$props3.protocol,
injectScript = _this$props3.injectScript,
rest = _objectWithoutProperties(_this$props3, ["url", "hideCaption", "maxWidth", "containerTagName", "onLoading", "onSuccess", "onAfterRender", "onFailure", "protocol", "injectScript"]);
return rest;
}
}, {
key: "injectScript",
value: function injectScript() {
var protocolToUse = window.location.protocol.indexOf('file') === 0 ? this.props.protocol : '';
var s = document.createElement('script');
s.async = s.defer = true;
s.src = "".concat(protocolToUse, "//platform.instagram.com/en_US/embeds.js");
s.id = 'react-instagram-embed-script';
var body = document.body;
if (body) {
body.appendChild(s);
}
}
}, {
key: "checkAPI",
value: function checkAPI() {
var _this4 = this;
return new Promise(function (resolve) {
(function checkAPI(_this) {
_this._timer = setTimeout(function () {
if (window.instgrm) {
clearTimeout(_this._timer);
resolve();
} else {
checkAPI(_this);
else {
if (this.props.injectScript && !document.getElementById('react-instagram-embed-script')) {
this.injectScript();
}
}, 20);
})(_this4);
});
}
}, {
key: "fetchEmbed",
value: function fetchEmbed(queryParams) {
this.request = this.createRequestPromise("https://api.instagram.com/oembed/?".concat(queryParams));
this.props.onLoading && this.props.onLoading();
this.request.promise.then(this.handleFetchSuccess).catch(this.handleFetchFailure);
}
}, {
key: "getQueryParams",
value: function getQueryParams(_ref) {
var url = _ref.url,
hideCaption = _ref.hideCaption,
maxWidth = _ref.maxWidth;
return qs.stringify({
url: url,
hidecaption: hideCaption,
maxwidth: typeof maxWidth === 'number' && maxWidth >= 320 ? maxWidth : undefined,
omitscript: true
});
}
}]);
return InstagramEmbed;
}(Component);
_defineProperty(InstagramEmbed, "defaultProps", {
hideCaption: false,
containerTagName: 'div',
protocol: 'https:',
injectScript: true
});
export { InstagramEmbed as default };
this.checkAPI().then(function () {
_this.fetchEmbed(_this.getQueryParams(_this.props));
});
}
};
InstagramEmbed.prototype.componentDidUpdate = function (prevProps) {
var _a = this.props, url = _a.url, hideCaption = _a.hideCaption, maxWidth = _a.maxWidth, containerTagName = _a.containerTagName;
if (prevProps.url !== url ||
prevProps.hideCaption !== hideCaption ||
prevProps.maxWidth !== maxWidth ||
prevProps.containerTagName !== containerTagName) {
this.request.cancel();
this.fetchEmbed(this.getQueryParams(this.props));
}
};
InstagramEmbed.prototype.componentWillUnmount = function () {
this.cancel();
};
InstagramEmbed.prototype.render = function () {
var Tag = this.props.containerTagName;
return React.createElement(Tag, __assign({}, this.omitComponentProps(), { dangerouslySetInnerHTML: { __html: this.state.html || '' } }));
};
InstagramEmbed.prototype.fetchEmbed = function (queryParams) {
this.request = this.createRequestPromise("https://api.instagram.com/oembed/?" + queryParams);
if (this.props.onLoading) {
this.props.onLoading();
}
this.request.promise.then(this.handleFetchSuccess).catch(this.handleFetchFailure);
};
InstagramEmbed.prototype.omitComponentProps = function () {
var _a = this.props, url = _a.url, hideCaption = _a.hideCaption, maxWidth = _a.maxWidth, containerTagName = _a.containerTagName, onLoading = _a.onLoading, onSuccess = _a.onSuccess, onAfterRender = _a.onAfterRender, onFailure = _a.onFailure, protocol = _a.protocol, injectScript = _a.injectScript, rest = __rest(_a, ["url", "hideCaption", "maxWidth", "containerTagName", "onLoading", "onSuccess", "onAfterRender", "onFailure", "protocol", "injectScript"]);
return rest;
};
InstagramEmbed.prototype.injectScript = function () {
var protocolToUse = window.location.protocol.indexOf('file') === 0 ? this.props.protocol : '';
var s = document.createElement('script');
s.async = s.defer = true;
s.src = protocolToUse + "//platform.instagram.com/en_US/embeds.js";
s.id = 'react-instagram-embed-script';
var body = document.body;
if (body) {
body.appendChild(s);
}
};
InstagramEmbed.prototype.checkAPI = function () {
var _this = this;
return new Promise(function (resolve) {
(function checkAPI(self) {
self.timer = window.setTimeout(function () {
if (window.instgrm) {
clearTimeout(self.timer);
resolve();
}
else {
checkAPI(self);
}
}, 20);
})(_this);
});
};
InstagramEmbed.prototype.getQueryParams = function (_a) {
var url = _a.url, hideCaption = _a.hideCaption, maxWidth = _a.maxWidth;
return qs.stringify({
url: url,
hidecaption: hideCaption,
maxwidth: typeof maxWidth === 'number' && maxWidth >= 320 ? maxWidth : undefined,
omitscript: true
});
};
InstagramEmbed.defaultProps = {
hideCaption: false,
containerTagName: 'div',
protocol: 'https:',
injectScript: true
};
return InstagramEmbed;
}(React.PureComponent));
export default InstagramEmbed;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _queryString = _interopRequireDefault(require("query-string"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var InstagramEmbed =
/*#__PURE__*/
function (_Component) {
_inherits(InstagramEmbed, _Component);
function InstagramEmbed() {
var _getPrototypeOf2;
var _this2;
_classCallCheck(this, InstagramEmbed);
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
_this2 = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(InstagramEmbed)).call.apply(_getPrototypeOf2, [this].concat(_args)));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "request", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "_timer", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "state", {
__html: null
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "handleFetchSuccess", function (response) {
_this2.props.onSuccess && _this2.props.onSuccess(response);
_this2.setState({
__html: response.html
}, function () {
window.instgrm.Embeds.process();
_this2.props.onAfterRender && _this2.props.onAfterRender();
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "handleFetchFailure", function () {
var _this2$props;
clearTimeout(_this2._timer);
_this2.props.onFailure && (_this2$props = _this2.props).onFailure.apply(_this2$props, arguments);
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "cancel", function () {
if (_this2.request) {
_this2.request.cancel();
}
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this2)), "createRequestPromise", function (url) {
for (var _len2 = arguments.length, opts = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
opts[_key2 - 1] = arguments[_key2];
}
var request = {};
request.promise = new Promise(function (resolve, reject) {
var promise = fetch(url, _objectSpread({}, opts)).then(function (response) {
return response.json();
}).then(function (json) {
return resolve(json);
}).catch(function (err) {
return reject(err);
});
request.cancel = function () {
return reject(new Error('Cancelled'));
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var qs = require("query-string");
var InstagramEmbed = /** @class */ (function (_super) {
__extends(InstagramEmbed, _super);
function InstagramEmbed(props) {
var _this = _super.call(this, props) || this;
_this.request = null;
// Public
_this.cancel = function () {
if (_this.request) {
_this.request.cancel();
}
};
return promise;
});
return request;
});
return _this2;
}
_createClass(InstagramEmbed, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this3 = this;
if (window.instgrm) {
this.fetchEmbed(this.getQueryParams(this.props));
} else {
if (this.props.injectScript && !document.getElementById('react-instagram-embed-script')) {
this.injectScript();
}
this.checkAPI().then(function () {
return _this3.fetchEmbed(_this3.getQueryParams(_this3.props));
});
}
_this.handleFetchSuccess = function (response) {
if (_this.props.onSuccess) {
_this.props.onSuccess(response);
}
_this.setState({ html: response.html }, function () {
window.instgrm.Embeds.process();
if (_this.props.onAfterRender) {
_this.props.onAfterRender();
}
});
};
_this.handleFetchFailure = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
clearTimeout(_this.timer);
if (_this.props.onFailure) {
_this.props.onFailure(args);
}
};
_this.createRequestPromise = function (url) {
var request = {};
request.promise = new Promise(function (resolve, reject) {
var promise = fetch(url)
.then(function (response) { return response.json(); })
.then(function (json) { return resolve(json); })
.catch(function (err) { return reject(err); });
request.cancel = function () { return reject(new Error('Cancelled')); };
return promise;
});
return request;
};
_this.state = { html: null };
return _this;
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props = this.props,
url = _this$props.url,
hideCaption = _this$props.hideCaption,
maxWidth = _this$props.maxWidth,
containerTagName = _this$props.containerTagName;
if (prevProps.url !== url || prevProps.hideCaption !== hideCaption || prevProps.maxWidth !== maxWidth || prevProps.containerTagName !== containerTagName) {
this.request.cancel();
this.fetchEmbed(this.getQueryParams(this.props));
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps, nextState) {
var _this$props2 = this.props,
url = _this$props2.url,
hideCaption = _this$props2.hideCaption,
maxWidth = _this$props2.maxWidth,
containerTagName = _this$props2.containerTagName,
onLoading = _this$props2.onLoading,
onSuccess = _this$props2.onSuccess,
onAfterRender = _this$props2.onAfterRender,
onFailure = _this$props2.onFailure;
var __html = this.state.__html;
if (nextProps.url !== url || nextProps.hideCaption !== hideCaption || nextProps.maxWidth !== maxWidth || nextProps.containerTagName !== containerTagName || nextProps.onLoading !== onLoading || nextProps.onSuccess !== onSuccess || nextProps.onAfterRender !== onAfterRender || nextProps.onFailure !== onFailure || nextState.__html !== __html) {
return true;
}
return false;
}
}, {
key: "render",
value: function render() {
var Tag = this.props.containerTagName;
return _react.default.createElement(Tag, _extends({}, this.omitComponentProps(), {
dangerouslySetInnerHTML: {
__html: this.state.__html
InstagramEmbed.prototype.componentDidMount = function () {
var _this = this;
if (window.instgrm) {
this.fetchEmbed(this.getQueryParams(this.props));
}
}));
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.cancel();
}
}, {
key: "omitComponentProps",
value: function omitComponentProps() {
// eslint-disable-next-line no-unused-vars
var _this$props3 = this.props,
url = _this$props3.url,
hideCaption = _this$props3.hideCaption,
maxWidth = _this$props3.maxWidth,
containerTagName = _this$props3.containerTagName,
onLoading = _this$props3.onLoading,
onSuccess = _this$props3.onSuccess,
onAfterRender = _this$props3.onAfterRender,
onFailure = _this$props3.onFailure,
protocol = _this$props3.protocol,
injectScript = _this$props3.injectScript,
rest = _objectWithoutProperties(_this$props3, ["url", "hideCaption", "maxWidth", "containerTagName", "onLoading", "onSuccess", "onAfterRender", "onFailure", "protocol", "injectScript"]);
return rest;
}
}, {
key: "injectScript",
value: function injectScript() {
var protocolToUse = window.location.protocol.indexOf('file') === 0 ? this.props.protocol : '';
var s = document.createElement('script');
s.async = s.defer = true;
s.src = "".concat(protocolToUse, "//platform.instagram.com/en_US/embeds.js");
s.id = 'react-instagram-embed-script';
var body = document.body;
if (body) {
body.appendChild(s);
}
}
}, {
key: "checkAPI",
value: function checkAPI() {
var _this4 = this;
return new Promise(function (resolve) {
(function checkAPI(_this) {
_this._timer = setTimeout(function () {
if (window.instgrm) {
clearTimeout(_this._timer);
resolve();
} else {
checkAPI(_this);
else {
if (this.props.injectScript && !document.getElementById('react-instagram-embed-script')) {
this.injectScript();
}
}, 20);
})(_this4);
});
}
}, {
key: "fetchEmbed",
value: function fetchEmbed(queryParams) {
this.request = this.createRequestPromise("https://api.instagram.com/oembed/?".concat(queryParams));
this.props.onLoading && this.props.onLoading();
this.request.promise.then(this.handleFetchSuccess).catch(this.handleFetchFailure);
}
}, {
key: "getQueryParams",
value: function getQueryParams(_ref) {
var url = _ref.url,
hideCaption = _ref.hideCaption,
maxWidth = _ref.maxWidth;
return _queryString.default.stringify({
url: url,
hidecaption: hideCaption,
maxwidth: typeof maxWidth === 'number' && maxWidth >= 320 ? maxWidth : undefined,
omitscript: true
});
}
}]);
return InstagramEmbed;
}(_react.Component);
this.checkAPI().then(function () {
_this.fetchEmbed(_this.getQueryParams(_this.props));
});
}
};
InstagramEmbed.prototype.componentDidUpdate = function (prevProps) {
var _a = this.props, url = _a.url, hideCaption = _a.hideCaption, maxWidth = _a.maxWidth, containerTagName = _a.containerTagName;
if (prevProps.url !== url ||
prevProps.hideCaption !== hideCaption ||
prevProps.maxWidth !== maxWidth ||
prevProps.containerTagName !== containerTagName) {
this.request.cancel();
this.fetchEmbed(this.getQueryParams(this.props));
}
};
InstagramEmbed.prototype.componentWillUnmount = function () {
this.cancel();
};
InstagramEmbed.prototype.render = function () {
var Tag = this.props.containerTagName;
return React.createElement(Tag, __assign({}, this.omitComponentProps(), { dangerouslySetInnerHTML: { __html: this.state.html || '' } }));
};
InstagramEmbed.prototype.fetchEmbed = function (queryParams) {
this.request = this.createRequestPromise("https://api.instagram.com/oembed/?" + queryParams);
if (this.props.onLoading) {
this.props.onLoading();
}
this.request.promise.then(this.handleFetchSuccess).catch(this.handleFetchFailure);
};
InstagramEmbed.prototype.omitComponentProps = function () {
var _a = this.props, url = _a.url, hideCaption = _a.hideCaption, maxWidth = _a.maxWidth, containerTagName = _a.containerTagName, onLoading = _a.onLoading, onSuccess = _a.onSuccess, onAfterRender = _a.onAfterRender, onFailure = _a.onFailure, protocol = _a.protocol, injectScript = _a.injectScript, rest = __rest(_a, ["url", "hideCaption", "maxWidth", "containerTagName", "onLoading", "onSuccess", "onAfterRender", "onFailure", "protocol", "injectScript"]);
return rest;
};
InstagramEmbed.prototype.injectScript = function () {
var protocolToUse = window.location.protocol.indexOf('file') === 0 ? this.props.protocol : '';
var s = document.createElement('script');
s.async = s.defer = true;
s.src = protocolToUse + "//platform.instagram.com/en_US/embeds.js";
s.id = 'react-instagram-embed-script';
var body = document.body;
if (body) {
body.appendChild(s);
}
};
InstagramEmbed.prototype.checkAPI = function () {
var _this = this;
return new Promise(function (resolve) {
(function checkAPI(self) {
self.timer = window.setTimeout(function () {
if (window.instgrm) {
clearTimeout(self.timer);
resolve();
}
else {
checkAPI(self);
}
}, 20);
})(_this);
});
};
InstagramEmbed.prototype.getQueryParams = function (_a) {
var url = _a.url, hideCaption = _a.hideCaption, maxWidth = _a.maxWidth;
return qs.stringify({
url: url,
hidecaption: hideCaption,
maxwidth: typeof maxWidth === 'number' && maxWidth >= 320 ? maxWidth : undefined,
omitscript: true
});
};
InstagramEmbed.defaultProps = {
hideCaption: false,
containerTagName: 'div',
protocol: 'https:',
injectScript: true
};
return InstagramEmbed;
}(React.PureComponent));
exports.default = InstagramEmbed;
_defineProperty(InstagramEmbed, "defaultProps", {
hideCaption: false,
containerTagName: 'div',
protocol: 'https:',
injectScript: true
});
{
"name": "react-instagram-embed",
"version": "1.4.2",
"version": "1.5.0",
"description": "React embedding Instagram posts component",

@@ -22,9 +22,12 @@ "main": "lib/index.js",

"precompile": "npm run clean:lib && npm run clean:es",
"compile:commonjs": "BABEL_ENV=commonjs babel -d lib src",
"compile:es": "BABEL_ENV=es babel -d es src",
"compile:commonjs": "tsc --outDir lib --module commonjs",
"compile:es": "tsc --build tsconfig.es.json",
"compile": "npm run compile:commonjs && npm run compile:es",
"lint": "eslint src test",
"test": "ava --verbose",
"test:coverage": "nyc npm test",
"type": "flow",
"_lint": "tslint -s node_modules/custom-tslint-formatters/formatters -t grouped",
"lint": "npm run _lint -- -p tsconfig.json",
"lint:fix": "npm run lint -- --fix",
"lint:lint-staged": "npm run _lint -- --fix",
"test": "jest --config jest.config.json",
"test:coverage": "npm test -- --coverage",
"coverage-report-upload": "codecov",
"clean:build": "if [ -d build ]; then rm -rf build; fi",

@@ -61,31 +64,34 @@ "build:demo": "NODE_ENV=production webpack"

"dependencies": {
"query-string": "^5.1.0"
"query-string": "^5.1.1"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.2.3",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/plugin-transform-flow-strip-types": "^7.2.3",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/polyfill": "^7.2.5",
"@babel/preset-env": "^7.2.3",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"ava": "^1.0.1",
"babel-eslint": "^10.0.1",
"@types/enzyme": "^3.1.15",
"@types/enzyme-adapter-react-16": "^1.0.3",
"@types/highlight.js": "^9.12.3",
"@types/jest": "^23.3.13",
"@types/query-string": "^6.2.0",
"@types/react": "^16.7.20",
"@types/react-dom": "^16.0.11",
"awesome-typescript-loader": "^5.2.1",
"babel-loader": "^8.0.5",
"browser-env": "^3.2.5",
"codecov": "^3.1.0",
"css-loader": "^2.1.0",
"eslint": "^5.12.0",
"eslint-plugin-ava": "^5.1.1",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-flowtype": "^3.2.1",
"eslint-plugin-react": "^7.12.3",
"flow-bin": "^0.66.0",
"custom-tslint-formatters": "^2.4.0",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"enzyme-to-json": "^3.3.5",
"gh-pages": "^1.2.0",
"highlight.js": "^9.12.0",
"highlight.js": "^9.13.1",
"html-webpack-plugin": "^3.2.0",
"husky": "^1.3.1",
"jest": "^23.6.0",
"lint-staged": "^8.1.0",
"normalize.css": "^8.0.1",
"nyc": "^13.1.0",
"raf": "^3.4.0",
"prettier": "^1.16.1",
"raf": "^3.4.1",
"react": "^16.7.0",

@@ -95,10 +101,15 @@ "react-dom": "^16.7.0",

"react-hot-loader": "^4.6.3",
"react-test-renderer": "^16.7.0",
"style-loader": "^0.23.1",
"ts-jest": "^23.10.5",
"tslint": "^5.12.1",
"tslint-config-prettier": "^1.17.0",
"tslint-eslint-rules": "^5.4.0",
"tslint-plugin-prettier": "^2.0.1",
"tslint-react": "^3.6.0",
"typescript": "^3.2.4",
"uglifyjs-webpack-plugin": "^2.1.1",
"webpack": "^4.28.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14",
"webpack-stylish": "^0.1.8"
"webpack-dev-server": "^3.1.14"
}
}

@@ -21,4 +21,4 @@ # react-instagram-embed

```js
import InstagramEmbed from 'react-instagram-embed'
```tsx
import InstagramEmbed from 'react-instagram-embed';

@@ -25,0 +25,0 @@ <InstagramEmbed

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