Skip to content

Commit 3a8a601

Browse files
🤖 Merge PR DefinitelyTyped#73762 [spf-parse] Add types for spf-parse by @ultraslayyy
Co-authored-by: Adam Thompson-Sharpe <[email protected]>
1 parent e095dc2 commit 3a8a601

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

types/spf-parse/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/spf-parse/index.d.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
export = spfParse;
2+
3+
declare namespace spfParse {
4+
/** Qualifer prefix characters */
5+
type Prefix = '+' | '-' | '~' | '?' | 'v';
6+
7+
/** Prefix descriptions */
8+
interface PrefixDescriptions {
9+
'+': 'Pass';
10+
'-': 'Fail';
11+
'~': 'SoftFail';
12+
'?': 'Neutral';
13+
v?: string;
14+
}
15+
16+
/** A single parsed SPF mechanism */
17+
interface Mechanism {
18+
/** Mechanism type, e.g. 'ip4', 'mx', 'all' */
19+
type: string;
20+
21+
/** Optional value associated with the mechanism (domain, IP, etc.) */
22+
value?: string;
23+
24+
/** The qualifier prefix */
25+
prefix: Prefix;
26+
27+
/** Human-readable description of the prefix */
28+
prefixdesc?: string;
29+
30+
/** Human-readable description of the mechanism */
31+
description?: string;
32+
}
33+
34+
/** Parse message (warning or error) */
35+
interface ParseMessage {
36+
/** Text of the message */
37+
message: string;
38+
39+
/** Message type: error or warning */
40+
type: 'error' | 'warning';
41+
}
42+
43+
/** Result of parsing a single SPF record */
44+
interface ParseResult {
45+
/** Parsed mechanisms */
46+
mechanisms: Mechanism[];
47+
48+
/** Validation messages */
49+
messages?: ParseMessage[];
50+
51+
/** True if record parsed successfully */
52+
valid: boolean;
53+
}
54+
55+
/** Error thrown by mechanism validators */
56+
class MechanismError extends Error {
57+
/** Error type */
58+
type: 'warning' | 'error';
59+
constructor(message: string, type?: 'warning' | 'error');
60+
}
61+
62+
/**
63+
* Parse a single SPF term (e.g. "+ip4:192.0.2.0/24")
64+
* @param term SPF term string
65+
* @param messages Message array to push validation errors/warnings into
66+
* @returns Parsed mechanism record
67+
*/
68+
function parseTerm(term: string, messages: ParseMessage[]): Mechanism;
69+
}
70+
71+
/**
72+
* Parse a full SPF record string (e.g. "v=spf1 include:some.sender.org -all")
73+
* @param record SPF record string
74+
* @returns Structured parse result
75+
*/
76+
declare function spfParse(record: string): spfParse.ParseResult;

types/spf-parse/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/spf-parse",
4+
"version": "1.0.9999",
5+
"projects": [
6+
"https://github.com/softvu/spf-parse#readme"
7+
],
8+
"devDependencies": {
9+
"@types/spf-parse": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "ultraslayyy",
14+
"githubUsername": "ultraslayyy"
15+
}
16+
]
17+
}

types/spf-parse/spf-parse-tests.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import spfParse = require('spf-parse');
2+
3+
const result = spfParse('v=spf1 a mx -all'); // $ExpectType ParseResult
4+
result.mechanisms; // $ExpectType Mechanism[]
5+
result.valid; // $ExpectType boolean
6+
7+
for (const mech of result.mechanisms) {
8+
mech.type; // $ExpectType string
9+
mech.value; // $ExpectType string | undefined
10+
mech.prefix; // $ExpectType Prefix
11+
mech.prefixdesc; // $ExpectType string | undefined
12+
mech.description; // $ExpectType string | undefined
13+
}
14+
15+
if (result.messages) {
16+
for (const msg of result.messages) {
17+
msg.message; // $ExpectType string
18+
msg.type; // $ ExpectType "error" | "warning"
19+
}
20+
}
21+
22+
const messages: spfParse.ParseMessage[] = [];
23+
const mech = spfParse.parseTerm('ip4:192.0.2.0/24', messages); // $ExpectType Mechanism
24+
25+
const err = new spfParse.MechanismError('invalid mech', 'error'); // $ExpectType MechanismError
26+
err.type; // $ExpectType "warning" | "error"

types/spf-parse/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"spf-parse-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)