TypeScript web-push setVapidDetails示例

说明

typescript web-push setvapiddetails示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: TypeScript

命名空间/包名称: web-push

示例#1
文件: web-push-tests.ts 项目: Jeremy-F/DefinitelyTyped

// ================
//  setGCMAPIKey()
// ================

// $ExpectType void
setGCMAPIKey('apiKey');
// $ExpectType void
setGCMAPIKey(null);

// ===================
//  setVapidDetails()
// ===================

// $ExpectType void
setVapidDetails('subject', 'privateKey', 'publicKey');

// Buffers are not supported here
// $ExpectError
setVapidDetails(buffer, 'publicKey', 'privateKey');
// $ExpectError
setVapidDetails('subject', buffer, 'privateKey');
// $ExpectError
setVapidDetails('subject', 'publicKey', buffer);

// ==================
//  PushSubscription
// ==================

const pushSubscription: PushSubscription = {
    endpoint: 'endpointString',

示例#2
文件: push-sw.ts 项目: ha-dai/Misskey

const push = require('web-push');
import * as mongo from 'mongodb';
import Subscription from './models/sw-subscription';
import config from './config';

if (config.sw) {
	// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
	push.setVapidDetails(
		config.maintainer.url,
		config.sw.public_key,
		config.sw.private_key);
}

export default async function(userId: mongo.ObjectID | string, type: string, body?: any) {
	if (!config.sw) return;

	if (typeof userId === 'string') {
		userId = new mongo.ObjectID(userId);
	}

	// Fetch
	const subscriptions = await Subscription.find({
		userId: userId
	});

	subscriptions.forEach(subscription => {
		const pushSubscription = {
			endpoint: subscription.endpoint,
			keys: {
				auth: subscription.auth,
				p256dh: subscription.publickey

展开阅读全文