Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type definitions for Keychain NPM module #64842

Merged
merged 8 commits into from Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions types/keychain/index.d.ts
@@ -0,0 +1,51 @@
// Type definitions for keychain 1.4
// Project: https://github.com/drudge/node-keychain
// Definitions by: Lucas Santos <https://github.com/khaosdoctor>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

declare namespace keychainTypes {
interface KeyChainBaseOptions {
account: string;
service: string;
password: string;
type?: 'generic' | 'internet';
}

type KeychainErrorCodes =
| 'UnsupportedPlatform'
| 'NoAccountProvided'
| 'NoServiceProvided'
| 'NoPasswordProvided'
| 'ServiceFailure'
| 'PasswordNotFound';

type KeychainErrorType = `${KeychainErrorCodes}Error`;

class KeychainError extends Error {
code: KeychainErrorCodes;
type: KeychainErrorType;
}
}

declare function getPassword(
options: Pick<keychainTypes.KeyChainBaseOptions, 'account' | 'service'>,
callback: (err: keychainTypes.KeychainError, password: string) => void,
): void;

declare function setPassword(
options: keychainTypes.KeyChainBaseOptions,
callback: (err: keychainTypes.KeychainError) => void,
): void;

declare function deletePassword(
options: Pick<keychainTypes.KeyChainBaseOptions, 'account' | 'service'>,
callback: (err: keychainTypes.KeychainError) => void,
): void;

declare const keychain: typeof keychainTypes & {
getPassword: typeof getPassword;
setPassword: typeof setPassword;
deletePassword: typeof deletePassword;
};

export = keychain;
74 changes: 74 additions & 0 deletions types/keychain/keychain-tests.ts
@@ -0,0 +1,74 @@
import keychain = require('keychain');

/**
* setPassword
*/

// @ts-expect-error
// Errors when doesn't have the required properties
keychain.setPassword({ account: 'some-account' }, err => {
if (err) {
err; // $ExpectType KeychainError
}
});

// @ts-expect-error
// Another error when missing options
keychain.setPassword({ account: 'some-account', password: 'some-pass' }, err => {
if (err) {
err; // $ExpectType KeychainError
}
});

// Should pass
keychain.setPassword({ account: 'some-account', password: 'some-pass', service: 'some-service' }, err => {
if (err) {
err; // $ExpectType KeychainError
}
});

/**
* getPassword
*/

// @ts-expect-error
// Errors when doesn't have the required properties
keychain.getPassword({ account: 'some-account' }, (err, password) => {
if (err) {
err; // $ExpectType KeychainError
return;
}

return password;
});

// Should pass
keychain.getPassword({ account: 'some-account', service: 'some-service' }, (err, password) => {
if (err) {
err; // $ExpectType KeychainError
return;
}

return password;
});

/**
* deletePassword
*/

// @ts-expect-error
// Errors when doesn't have the required properties
keychain.deletePassword({ account: 'some-account' }, err => {
if (err) {
err; // $ExpectType KeychainError
return;
}
});

// Should pass
keychain.getPassword({ account: 'some-account', service: 'some-service' }, err => {
if (err) {
err; // $ExpectType KeychainError
return;
}
});
23 changes: 23 additions & 0 deletions types/keychain/tsconfig.json
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"keychain-tests.ts"
]
}
3 changes: 3 additions & 0 deletions types/keychain/tslint.json
@@ -0,0 +1,3 @@
{
"extends": "@definitelytyped/dtslint/dt.json"
}