Upgrade Mux SDK to v8

JavaScript pattern

Upgrade the Mux SDK to v8 with Stainless

Creating Mux instance

BEFORE
const Mux = require('@mux/mux-node');

const { Video, Data } = new Mux({
  baseUrl: 'test.com',
  platform: {
    name: 'Test',
    version: '0.0.1',
  },
});

const { Video, Data } = new Mux(accessToken, secret);

const { Video, Data } = new Mux();

const { Video, Data } = new Mux(accessToken, secret, {
  baseUrl: 'test.com',
  platform: {
    name: 'Test',
    version: '0.0.1',
  },
});
AFTER
const Mux = require('@mux/mux-node');

const mux = new Mux({
  baseURL: 'test.com',
  fetch: (url, opts) => {
    let opts = opts ?? { headers: {} };

    opts.headers['x-source-platform'] = 'Test | 0.0.1';

    return fetch(url, opts);
  },
});

const mux = new Mux({
  tokenId: accessToken,
  tokenSecret: secret,
});

const mux = new Mux();

const mux = new Mux({
  tokenId: accessToken,
  tokenSecret: secret,
  baseURL: 'test.com',
  fetch: (url, opts) => {
    let opts = opts ?? { headers: {} };

    opts.headers['x-source-platform'] = 'Test | 0.0.1';

    return fetch(url, opts);
  },
});

Replace destructured properties with field access

BEFORE
import Mux from '@mux/mux-node';

const { Video, Data } = new Mux();

const upload = await Video.Uploads.create({
  new_asset_settings: { playback_policy: 'public' },
  cors_origin: '*',
});

const assets = await Video.Assets.create({ playback_policy: 'public' }, {});

const breakdown = await Data.Metrics.breakdown('aggregate_startup_time', {
  group_by: 'browser',
});

const usage = await Video.LiveStreams.create({});
AFTER
import Mux from '@mux/mux-node';

const mux = new Mux();

const upload = await mux.video.uploads.create({
  new_asset_settings: { playback_policy: ['public'] },
  cors_origin: '*',
});

const assets = await mux.video.assets.create({ playback_policy: ['public'] }, {});

const breakdown = await mux.data.metrics.breakdown('aggregate_startup_time', {
  group_by: 'browser',
});

const usage = await mux.video.liveStreams.create({});

No import fixes

BEFORE
const { Video, Data } = new Mux();
AFTER
const mux = new Mux();

Renamed .get to .retrieve

BEFORE
const { Video, Data } = new Mux();

const asset = await Video.Assets.get(req.query.id as string);
const upload = await Video.Uploads.get(req.query.id as string);
AFTER
const mux = new Mux();

const asset = await mux.video.assets.retrieve(req.query.id as string);
const upload = await mux.video.uploads.retrieve(req.query.id as string);

Renamed .del to .delete

BEFORE
const { Video, Data } = new Mux();

await Video.Assets.del(req.query.id as string);
AFTER
const mux = new Mux();

await mux.video.assets.delete(req.query.id as string);

Replace verifyHeader with verifySignature

BEFORE
Mux.Webhooks.verifyHeader(rawBody, req.headers['mux-signature'] as string, webhookSignatureSecret);
AFTER
const mux = new Mux();
mux.webhooks.verifyHeader(
  Buffer.isBuffer(rawBody) ? rawBody.toString('utf8') : rawBody,
  req.headers,
  webhookSignatureSecret,
);

Verify webhooks with existing Mux instance

If there is an existing Mux instance in the file, the webhook verifier should use it.

BEFORE
const Mux = require('@mux/mux-node');

const { Video, Data } = new Mux({
  baseUrl: 'test.com',
});

export const verifyWebhookSignature = (rawBody: string | Buffer, req: NextApiRequest) => {
  if (webhookSignatureSecret) {
    Mux.Webhooks.verifyHeader(rawBody, req.headers['mux-signature'] as string, webhookSignatureSecret);
  }
};
AFTER
const Mux = require('@mux/mux-node');

const mux = new Mux({
  baseURL: 'test.com',
});

export const verifyWebhookSignature = (rawBody: string | Buffer, req: NextApiRequest) => {
  if (webhookSignatureSecret) {
    mux.webhooks.verifyHeader(
      Buffer.isBuffer(rawBody) ? rawBody.toString('utf8') : rawBody,
      req.headers,
      webhookSignatureSecret,
    );
  }
};