-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): change all strategies from stamps to TypeScript clas…
…ses (#4103) Refs #3481 BREAKING CHANGE: all strategies from apidom-reference package became a class and requires to be instantiated with new operator.
- Loading branch information
Showing
28 changed files
with
767 additions
and
830 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"class-methods-use-this": 0, | ||
"@typescript-eslint/naming-convention": 0 | ||
} | ||
} |
34 changes: 16 additions & 18 deletions
34
packages/apidom-reference/src/bundle/strategies/BundleStrategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import stampit from 'stampit'; | ||
import { NotImplementedError } from '@swagger-api/apidom-error'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
|
||
import { BundleStrategy as IBundleStrategy } from '../../types'; | ||
import File from '../../File'; | ||
import type { ReferenceOptions } from '../../options'; | ||
|
||
const BundleStrategy: stampit.Stamp<IBundleStrategy> = stampit({ | ||
props: { | ||
name: null, | ||
}, | ||
methods: { | ||
canBundle() { | ||
return false; | ||
}, | ||
export interface BundleStrategyOptions { | ||
readonly name: string; | ||
} | ||
|
||
async bundle(): Promise<never> { | ||
throw new NotImplementedError( | ||
'bundle method in BundleStrategy stamp is not yet implemented.', | ||
); | ||
}, | ||
}, | ||
}); | ||
abstract class BundleStrategy { | ||
public readonly name: string; | ||
|
||
constructor({ name }: BundleStrategyOptions) { | ||
this.name = name; | ||
} | ||
|
||
abstract canBundle(file: File, options: ReferenceOptions): boolean; | ||
abstract bundle(file: File, options: ReferenceOptions): Promise<ParseResultElement>; | ||
} | ||
|
||
export default BundleStrategy; |
44 changes: 21 additions & 23 deletions
44
packages/apidom-reference/src/bundle/strategies/openapi-3-1/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
import stampit from 'stampit'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { mediaTypes, isOpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
|
||
import File from '../../../File'; | ||
import BundleStrategy from '../BundleStrategy'; | ||
import { BundleStrategy as IBundleStrategy } from '../../../types'; | ||
import BundleStrategy, { BundleStrategyOptions } from '../BundleStrategy'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const OpenApi3_1BundleStrategy: stampit.Stamp<IBundleStrategy> = stampit(BundleStrategy, { | ||
init() { | ||
this.name = 'openapi-3-1'; | ||
}, | ||
methods: { | ||
canBundle(file: File): boolean { | ||
// assert by media type | ||
if (file.mediaType !== 'text/plain') { | ||
return mediaTypes.includes(file.mediaType); | ||
} | ||
export interface OpenAPI3_1BundleStrategyOptions extends Omit<BundleStrategyOptions, 'name'> {} | ||
|
||
// assert by inspecting ApiDOM | ||
return isOpenApi3_1Element(file.parseResult?.result); | ||
}, | ||
class OpenAPI3_1BundleStrategy extends BundleStrategy { | ||
constructor(options?: OpenAPI3_1BundleStrategyOptions) { | ||
super({ ...(options ?? {}), name: 'openapi-3-1' }); | ||
} | ||
|
||
async bundle(file: File): Promise<ParseResultElement> { | ||
return file.parseResult!; | ||
}, | ||
}, | ||
}); | ||
canBundle(file: File): boolean { | ||
// assert by media type | ||
if (file.mediaType !== 'text/plain') { | ||
return mediaTypes.includes(file.mediaType); | ||
} | ||
|
||
export default OpenApi3_1BundleStrategy; | ||
// assert by inspecting ApiDOM | ||
return isOpenApi3_1Element(file.parseResult?.result); | ||
} | ||
|
||
async bundle(file: File): Promise<ParseResultElement> { | ||
return file.parseResult!; | ||
} | ||
} | ||
|
||
export default OpenAPI3_1BundleStrategy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/apidom-reference/src/dereference/strategies/.eslintrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"class-methods-use-this": 0, | ||
"@typescript-eslint/naming-convention": 0 | ||
} | ||
} |
34 changes: 16 additions & 18 deletions
34
packages/apidom-reference/src/dereference/strategies/DereferenceStrategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import stampit from 'stampit'; | ||
import { NotImplementedError } from '@swagger-api/apidom-error'; | ||
import { Element } from 'minim'; | ||
|
||
import { DereferenceStrategy as IDereferenceStrategy } from '../../types'; | ||
import File from '../../File'; | ||
import type { ReferenceOptions } from '../../options'; | ||
|
||
const DereferenceStrategy: stampit.Stamp<IDereferenceStrategy> = stampit({ | ||
props: { | ||
name: null, | ||
}, | ||
methods: { | ||
canDereference() { | ||
return false; | ||
}, | ||
export interface DereferenceStrategyOptions { | ||
readonly name: string; | ||
} | ||
|
||
async dereference(): Promise<never> { | ||
throw new NotImplementedError( | ||
'dereference method in DereferenceStrategy stamp is not yet implemented.', | ||
); | ||
}, | ||
}, | ||
}); | ||
abstract class DereferenceStrategy { | ||
public readonly name: string; | ||
|
||
constructor({ name }: DereferenceStrategyOptions) { | ||
this.name = name; | ||
} | ||
|
||
abstract canDereference(file: File, options: ReferenceOptions): boolean; | ||
abstract dereference(file: File, options: ReferenceOptions): Promise<Element>; | ||
} | ||
|
||
export default DereferenceStrategy; |
Oops, something went wrong.