gplayapi-ts
Version:
Google Play API wrapper in TypeScript
170 lines • 5.84 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var client_exports = {};
__export(client_exports, {
GooglePlayClient: () => GooglePlayClient
});
module.exports = __toCommonJS(client_exports);
var import_fs = require("fs");
var import_factory = require("../devices/factory");
var import_errors = require("../errors");
var import_network = require("../utils/network");
var import_parser = require("../utils/parser");
var import_auth = require("./endpoints/auth");
var import_apps = require("./endpoints/apps");
var import_library = require("./endpoints/library");
var import_common = require("../models/common");
class GooglePlayClient {
authData;
deviceInfo;
sessionFile;
auth;
apps;
library;
constructor(authData, deviceInfo = import_factory.Pixel3a) {
this.authData = authData;
this.deviceInfo = deviceInfo;
this.auth = new import_auth.AuthEndpoint(
this.authData,
this.deviceInfo,
() => this.getAuthHeaders(),
(url, options) => this.doAuthedReq(url, options)
);
this.apps = new import_apps.AppsEndpoint(
(url, options) => this.doAuthedReq(url, options)
);
this.library = new import_library.LibraryEndpoint(
(url, options) => this.doAuthedReq(url, options),
(packageName) => this.getAppDetails(packageName)
);
}
static async newClient(email, aasToken) {
return GooglePlayClient.newClientWithDeviceInfo(email, aasToken, import_factory.Pixel3a);
}
static async newClientWithDeviceInfo(email, aasToken, deviceInfo) {
const authData = {
email,
aasToken,
locale: "en_GB"
};
const client = new GooglePlayClient(authData, deviceInfo);
await client.auth.generateGsfId();
const deviceConfigRes = await client.auth.uploadDeviceConfig();
authData.deviceConfigToken = deviceConfigRes.uploadDeviceConfigToken;
const token = await client.auth.generateGPToken();
authData.authToken = token;
await client.auth.toc();
return client;
}
async saveSession(file) {
await import_fs.promises.writeFile(file, JSON.stringify(this.authData, null, 2));
}
static async loadSession(file) {
return GooglePlayClient.loadSessionWithDeviceInfo(file, import_factory.Pixel3a);
}
static async loadSessionWithDeviceInfo(file, deviceInfo) {
const data = await import_fs.promises.readFile(file, "utf-8");
const authData = JSON.parse(data);
const client = new GooglePlayClient(authData, deviceInfo);
return client;
}
async _doAuthedReq(url, options = {}) {
const { data, status } = await (0, import_network.doRequest)(url, {
...options,
headers: {
...this.getDefaultHeaders(),
...options.headers || {}
}
});
if (status === import_common.HttpStatusCode.UNAUTHORIZED) {
throw new import_errors.GPTokenExpiredError();
}
if (status >= 400) {
throw new import_errors.GPNetworkError(`Request failed with status ${status}`, status);
}
const resp = await (0, import_parser.unmarshalResponseWrapper)(data);
return resp.payload || null;
}
async doAuthedReq(url, options = {}) {
try {
return await this._doAuthedReq(url, options);
} catch (error) {
if (error instanceof import_errors.GPTokenExpiredError) {
await this.auth.regenerateGPToken();
if (this.sessionFile) {
await this.saveSession(this.sessionFile);
}
return await this._doAuthedReq(url, options);
}
throw error;
}
}
getAuthHeaders() {
return {
App: "com.google.android.gms",
"User-Agent": this.deviceInfo.authUserAgent,
...this.authData.gsfId && { Device: this.authData.gsfId }
};
}
getDefaultHeaders() {
const data = this.authData;
const headers = (0, import_network.createDefaultHeaders)(
this.deviceInfo.userAgent,
data.gsfId,
data.authToken
);
if (data.deviceCheckInConsistencyToken) {
headers["X-Dfe-Device-Checkin-Consistency-Token"] = data.deviceCheckInConsistencyToken;
}
if (data.deviceConfigToken) {
headers["X-Dfe-Device-Config-Token"] = data.deviceConfigToken;
}
if (data.dfeCookie) {
headers["X-Dfe-Cookie"] = data.dfeCookie;
}
if (this.deviceInfo.simOperator) {
headers["X-Dfe-Mccmnc"] = this.deviceInfo.simOperator;
}
return headers;
}
// Convenience methods
async getAppDetails(packageName) {
return this.apps.getAppDetails(packageName);
}
async getBuyResponse(packageName, version) {
return this.library.getBuyResponse(packageName, version);
}
async getDeliveryResponse(packageName, version) {
return this.library.getDeliveryResponse(packageName, version);
}
async purchase(packageName, version) {
return this.library.purchase(packageName, version);
}
async generateGsfId() {
return this.auth.generateGsfId();
}
async regenerateGPToken() {
return this.auth.regenerateGPToken();
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
GooglePlayClient
});
//# sourceMappingURL=client.js.map