DefinitelyTyped/types/node-easy-cert/node-easy-cert-tests.ts
Maxime LUCE 62cf51a6f2 Add node-easy-cert typings (#38877)
* Add node-easy-cert typings

* Fix requested reviews
2019-10-14 12:48:44 -07:00

83 lines
1.5 KiB
TypeScript

import CertManager = require('node-easy-cert');
const options = {
rootDirPath: '/the/full/path/of/the/dir',
defaultCertAttrs: [
{ name: 'countryName', value: 'CN' },
{ name: 'organizationName', value: 'CertManager' },
{ shortName: 'ST', value: 'SH' },
{ shortName: 'OU', value: 'CertManager SSL' },
],
};
const crtMgr = new CertManager(options);
/*
* generateRootCA
*/
const rootCaOptions = {
commonName: 'the-name-you-like',
};
crtMgr.generateRootCA(rootCaOptions, (err, keyPath, certPath) => {
if (err === 'ROOT_CA_EXISTED') {
// log that overwrite should be specified to force generation
}
if (err === 'ROOT_CA_COMMON_NAME_UNSPECIFIED') {
// can't append in typescript :)
}
// log keyPath and certPath to allow users to trust them
});
/*
* getCertificate
*/
crtMgr.getCertificate('localhost', (err, keyContent, crtContent) => {
if (err === 'ROOT_CA_NOT_EXISTS') {
// log that the user should call generateRootCA before trying to generate certificates.
}
// log keyContent, crtContent
});
/*
* getRootDirPath
*/
const rootPath = crtMgr.getRootDirPath();
/*
* getRootCAFilePath
*/
const rootCAFile = crtMgr.getRootCAFilePath();
/*
* isRootCAFileExists
*/
if (crtMgr.isRootCAFileExists()) {
// generate certs
}
/*
* ifRootCATrusted
*/
if (!crtMgr.ifRootCATrusted()) {
// ask user to trust CA certificate
}
/*
* clearCerts
*/
crtMgr.clearCerts();
crtMgr.clearCerts(() => {
// all certificates cleared!
});