Skip to content

TypeScript

Node-APK is written in TypeScript and provides comprehensive type definitions out of the box.

Type Safety

All public APIs are fully typed:

typescript
import { Apk, Manifest, Certificate, Resources } from "node-apk";

const apk: Apk = new Apk("app.apk");
const manifest: Manifest = await apk.getManifestInfo();
const certs: Certificate[] = await apk.getCertificateInfo();
const resources: Resources = await apk.getResources();

Available Types

Main Classes

typescript
import {
  Apk,          // Main APK parser class
  Manifest,     // Parsed manifest information
  Receiver,     // Broadcast receiver information
  Certificate,  // Signing certificate
  Resources,    // Resource table parser
  Resource,     // Individual resource value
  XmlElement,   // Binary XML element
  Source,       // Binary data source
  ZipEntry,     // ZIP entry handler
} from "node-apk";

Utility Types

typescript
import {
  Locale,           // Resource locale information
  ChunkType,        // Binary chunk types
  ResourceValue,    // Union type for resource values
  LabelOptions,     // Options for getLabel()
  IconOptions,      // Options for getLauncherIcon()
  IconDensity,      // Icon density values
  BufferLoader,     // Function type for buffer loading
} from "node-apk";

Type Definitions

ResourceValue

typescript
type ResourceValue = number | string | boolean | null;

LabelOptions

typescript
interface LabelOptions {
  /** Preferred locale for the label (e.g., "en", "fr", "zh") */
  locale?: string;
}

IconOptions

typescript
interface IconOptions {
  /** Preferred icon density */
  density?: IconDensity;
}

IconDensity

typescript
type IconDensity = "mdpi" | "hdpi" | "xhdpi" | "xxhdpi" | "xxxhdpi" | "nodpi" | "anydpi";

Locale

typescript
class Locale {
  /** Two-letter language code (e.g., "en", "fr") */
  readonly language?: string;
  
  /** Two-letter country code (e.g., "US", "FR") */
  readonly country?: string;
}

Generic Usage Examples

Function with Apk Parameter

typescript
import { Apk, Manifest } from "node-apk";

async function getPackageInfo(apk: Apk): Promise<{
  package: string;
  version: string;
}> {
  const manifest = await apk.getManifestInfo();
  return {
    package: manifest.package,
    version: manifest.versionName,
  };
}

Working with Resources

typescript
import { Resources, Resource, ResourceValue } from "node-apk";

function getResourceValue(resource: Resource): ResourceValue {
  return resource.value;
}

function getLocaleCode(resource: Resource): string | undefined {
  return resource.locale?.language;
}

Certificate Chain Processing

typescript
import { Certificate } from "node-apk";

function validateCertificateChain(cert: Certificate): boolean {
  // Check if certificate is self-signed
  if (!cert.parent) {
    console.log("Self-signed certificate");
    return true;
  }
  
  // Validate chain
  const chain = cert.chain;
  console.log(`Certificate chain length: ${chain.length}`);
  
  return chain.every(c => c.validUntil > new Date());
}

Strict Mode

Node-APK works well with TypeScript strict mode:

json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitReturns": true
  }
}

Example with strict null checks:

typescript
import { Apk } from "node-apk";

const apk = new Apk("app.apk");
const certs = await apk.getCertificateInfo();

// Type-safe access to first certificate
if (certs.length > 0) {
  const cert = certs[0]; // Type: Certificate (not Certificate | undefined)
  console.log(cert.subject.get("CN"));
}

Module Resolution

For proper type resolution, use NodeNext module resolution:

json
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

IDE Support

Node-APK provides full IDE support:

  • Autocomplete - All methods and properties are suggested
  • Type hints - Hover over any identifier to see its type
  • Documentation - JSDoc comments appear in IDE tooltips
  • Go to definition - Navigate to source code for any type

Best Practices

1. Use Type Annotations

typescript
// ✅ Good - explicit types
const manifest: Manifest = await apk.getManifestInfo();

// ❌ Avoid - implicit any
const manifest = await apk.getManifestInfo();

2. Handle Nullable Types

typescript
// ✅ Good - handle optional locale
const locale = resource.locale;
const lang = locale?.language ?? "unknown";

// ❌ Avoid - assumes locale exists
const lang = resource.locale.language;

3. Use Type Guards

typescript
function isStringResource(value: ResourceValue): value is string {
  return typeof value === "string";
}

const value = resources.resolve(labelId)[0]?.value;
if (isStringResource(value)) {
  console.log(value.toUpperCase()); // Type: string
}

Next Steps

Released under the MIT License.