Skip to content

Commit e2f9e98

Browse files
KennethBWSongSLdragonwenytang-ms
authored
perf: spec-parser to separate package (OfficeDev#10842)
* perf: init spec-parser package * perf(spec-parser): remove unused package, use correct option (OfficeDev#10776) Co-authored-by: turenlong <[email protected]> * perf: add test (OfficeDev#10789) * perf: add test files * perf: update test * test: add browser test (OfficeDev#10796) * test: add browser test * perf: remove useless code * test: fix coverage = 0 bug (OfficeDev#10802) * perf: not allow res/req body has type other than json (OfficeDev#10803) Co-authored-by: turenlong <[email protected]> * ci: add ci for spec-parser (OfficeDev#10805) * ci: add ci for spec-parser * test: update test * test: update test * test: update dependency * ci: add cd for spec parser (OfficeDev#10816) * ci: add cd for spec parser * fix: fix pacakge * test: fix ut * ci: fix cd for spec parser (OfficeDev#10819) * ci: update node version to 18 (OfficeDev#10820) * perf(spec-parser): try to fix browser lib issue (OfficeDev#10821) * perf: try to fix browser lib issue * perf: add missing constant export --------- Co-authored-by: turenlong <[email protected]> * ci: update version (OfficeDev#10822) * perf: add format for spec parser (OfficeDev#10827) * perf: add format for spec parser * fix: add eslint file * build: update lock file * perf: export utils in spec parser (OfficeDev#10829) * perf: add license and update package.json (OfficeDev#10837) * perf: add license and update package.json * ci: update version * perf(spec-parser): add README.md and update export (OfficeDev#10838) Co-authored-by: turenlong <[email protected]> * ci: update version (OfficeDev#10839) * perf: init spec-parser package * perf(spec-parser): remove unused package, use correct option (OfficeDev#10776) Co-authored-by: turenlong <[email protected]> * perf: add test (OfficeDev#10789) * perf: add test files * perf: update test * test: add browser test (OfficeDev#10796) * test: add browser test * perf: remove useless code * test: fix coverage = 0 bug (OfficeDev#10802) * perf: not allow res/req body has type other than json (OfficeDev#10803) Co-authored-by: turenlong <[email protected]> * ci: add ci for spec-parser (OfficeDev#10805) * ci: add ci for spec-parser * test: update test * test: update test * test: update dependency * ci: add cd for spec parser (OfficeDev#10816) * ci: add cd for spec parser * fix: fix pacakge * test: fix ut * ci: fix cd for spec parser (OfficeDev#10819) * ci: update node version to 18 (OfficeDev#10820) * perf(spec-parser): try to fix browser lib issue (OfficeDev#10821) * perf: try to fix browser lib issue * perf: add missing constant export --------- Co-authored-by: turenlong <[email protected]> * ci: update version (OfficeDev#10822) * perf: add format for spec parser (OfficeDev#10827) * perf: add format for spec parser * fix: add eslint file * build: update lock file * perf: export utils in spec parser (OfficeDev#10829) * perf: add license and update package.json (OfficeDev#10837) * perf: add license and update package.json * ci: update version * perf(spec-parser): add README.md and update export (OfficeDev#10838) Co-authored-by: turenlong <[email protected]> * ci: update version (OfficeDev#10839) * perf: update * docs: update readme * perf: update package.json * test: fix test * perf: update pnpm-lock * test: fix ut * build: update config in webpack * fix: fix format --------- Co-authored-by: rentu <[email protected]> Co-authored-by: turenlong <[email protected]> Co-authored-by: wenyutang-ms <[email protected]>
1 parent 670e706 commit e2f9e98

40 files changed

+17037
-4
lines changed

.github/workflows/spec-parser-cd.yml

+55-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,64 @@ run-name: Spec-Parser-${{ github.run_id }}-${{ github.ref_name }}
33

44
on:
55
workflow_dispatch:
6+
inputs:
7+
preid:
8+
description: "preid name(alpha, rc, stable)"
9+
required: true
10+
default: "alpha"
611

712
jobs:
813
cd:
914
runs-on: ubuntu-latest
1015
steps:
11-
- name: Output
12-
if: ${{ github.event_name == 'workflow_dispatch' && github.ref != 'refs/heads/spec-parser' }}
16+
- name: Checkout
17+
uses: actions/checkout@v3
18+
19+
- name: Setup node
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: 18
23+
24+
- name: Install
25+
working-directory: packages/spec-parser/
26+
run: |
27+
npm install
28+
29+
- name: Build
30+
working-directory: packages/spec-parser/
31+
run: |
32+
npm run build
33+
34+
- name: Setup npm registry
35+
run: |
36+
echo "${{ secrets.NPMRC }}" > ~/.npmrc
37+
38+
- name: Version alpha
39+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.preid == 'alpha' }}
40+
working-directory: packages/spec-parser/
41+
run: |
42+
npm version prerelease --preid=alpha
43+
44+
- name: Version rc
45+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.preid == 'rc' }}
46+
working-directory: packages/spec-parser/
47+
run: |
48+
npm version prerelease --preid=rc
49+
50+
- name: publish alpha release to npm org
51+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.preid == 'alpha' }}
52+
working-directory: packages/spec-parser/
53+
run: |
54+
npm publish --tag alpha
55+
56+
- name: publish rc release to npm org
57+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.preid == 'rc' }}
58+
working-directory: packages/spec-parser/
59+
run: |
60+
npm publish --tag rc
61+
62+
- name: publish stable release to npm org
63+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.preid == 'stable' }}
64+
working-directory: packages/spec-parser/
1365
run: |
14-
echo It's not allowed to run CD on other branch except spec-parser.
15-
exit 1
66+
npm publish

.github/workflows/spec-parser-ci.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Spec-Parser CI
2+
3+
on:
4+
push:
5+
paths:
6+
- "packages/spec-parser/**"
7+
branches: ["main", "dev", "prerelease", "spec-parser"]
8+
pull_request:
9+
paths:
10+
- "packages/spec-parser/**"
11+
branches: ["main", "dev", "prerelease", "spec-parser"]
12+
workflow_dispatch:
13+
14+
jobs:
15+
test:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
22+
- name: Setup node
23+
uses: actions/setup-node@v3
24+
with:
25+
node-version: ${{ matrix.version }}
26+
27+
- name: Install
28+
working-directory: packages/spec-parser/
29+
run: |
30+
npm install
31+
32+
- name: Test
33+
working-directory: packages/spec-parser/
34+
run: |
35+
npm run test:unit

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This repository contains the following packages:
5656
| **API** [packages/api](https://github.com/OfficeDev/TeamsFx/tree/main/packages/api) | The API package is a collection of contracts supported by the IDE Extensions and CLI. It enables developers to write plugins to extend TeamsFx with new capabilities. |
5757
| **Core** [packages/fx-core](https://github.com/OfficeDev/TeamsFx/tree/main/packages/fx-core) | The Core package centralizes implementation of capabilities shared by the IDE Extensions and the CLI. |
5858
| **Azure Functions Support** [packages/function-extension](https://github.com/OfficeDev/TeamsFx/tree/main/packages/function-extension) | Teams Toolkit helps developers include server-side code in their Teams application backed by [Azure Functions](https://docs.microsoft.com/azure/azure-functions/). This plugin adds support to simplify the integration of an authentication-aware Azure Function into your Teams app. |
59+
| **Spec Parser** [packages/spec-parser](https://github.com/OfficeDev/TeamsFx/tree/main/packages/spec-parser) | Teams Toolkit automates the process of generating [API-based message extensions](https://learn.microsoft.com/en-us/microsoftteams/platform/messaging-extensions/api-based-overview) and Adaptive Cards by parsing the OpenAPI description document. |
5960

6061
## Contributions
6162

packages/spec-parser/.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist*/*

packages/spec-parser/.eslintrc.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
parser: "@typescript-eslint/parser",
3+
parserOptions: {
4+
tsconfigRootDir: __dirname,
5+
},
6+
extends: ["../eslint-plugin-teamsfx/config/shared.js"],
7+
overrides: [
8+
{
9+
files: ["src/*.ts"],
10+
extends: [
11+
"../eslint-plugin-teamsfx/config/header.js",
12+
"../eslint-plugin-teamsfx/config/promise.js",
13+
"../eslint-plugin-teamsfx/config/type.js",
14+
],
15+
},
16+
],
17+
};

packages/spec-parser/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Build results
2+
dist/
3+
types/
4+
coverage-browser/
5+
test-results*.xml
6+
node_modules/
7+
*.tgz

packages/spec-parser/.nycrc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "@istanbuljs/nyc-config-typescript",
3+
"all": true,
4+
"include": [
5+
"src/**/*.ts"
6+
],
7+
"exclude": [
8+
"src/**/*.browser.ts"
9+
],
10+
"reporter": [
11+
"text",
12+
"html",
13+
"json-summary",
14+
"cobertura",
15+
"lcov"
16+
],
17+
"check-coverage": true,
18+
"lines": 0
19+
}

packages/spec-parser/.prettierignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**/src/generated/
2+
**/*.d.ts
3+
4+
coverage/
5+
.nyc_output/
6+
.prettierignore

packages/spec-parser/.prettierrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require("../prettier-config"),
3+
};

packages/spec-parser/LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) Microsoft Corporation.
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/spec-parser/README.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# M365 Spec Parser
2+
3+
The M365 Spec Parser package is designed to parse OpenAPI specification files to generate resources for M365 applications.
4+
5+
## Sample Usage
6+
7+
### For NodeJS environment
8+
```typescript
9+
import { SpecParser, ParseOptions } from "@microsoft/m365-spec-parser";
10+
11+
// Define parsing options
12+
const option: ParseOptions = {
13+
allowMissingId: true, // Allow missing IDs in the specification, default true
14+
allowSwagger: true, // Allow Swagger specifications, default true
15+
allowAPIKeyAuth: false, // Disallow API key authentication, default false
16+
allowMultipleParameters: false, // Disallow multiple parameters, default false
17+
allowOauth2: false, // Disallow OAuth2 authentication, default false
18+
};
19+
20+
// Create a new SpecParser instance with the given options
21+
const parser = new SpecParser("path/to/spec/file", option);
22+
23+
// Validate the specification
24+
const validateResult = await parser.validate();
25+
// If the specification is not valid, log the errors and warnings
26+
if (validateResult.status !== ValidationStatus.Valid) {
27+
console.log(validateResult.errors);
28+
console.log(validateResult.warnings);
29+
}
30+
31+
// List the operations in the specification
32+
const listResult = await parser.list();
33+
// Log each operation
34+
for (let i = 0; i < listResult.length; i++) {
35+
console.log(listResult[i]);
36+
}
37+
38+
// Define a filter for the operations to generate
39+
const filter = ["GET /pet/{id}"];
40+
// Define the paths for the Teams app manifest file, the output specification file, and the output adaptive card folder
41+
const teamsAppManifestFilePath = "path/to/teamsapp/manifest/file";
42+
const outputSpecFilePath = "path/to/output/spec/path";
43+
const outputAdaptiveCardFolder = "adaptivecard/folder";
44+
// Generate the operations
45+
const generateResult = await parser.generate(
46+
teamsAppManifestFilePath,
47+
filter,
48+
outputSpecFilePath,
49+
outputAdaptiveCardFolder
50+
);
51+
52+
// If not all operations were successfully generated, log the warnings
53+
if (!generateResult.allSuccess) {
54+
console.log(generateResult.warnings);
55+
}
56+
```
57+
58+
### For browser environment
59+
```typescript
60+
import { SpecParser, ParseOptions } from "@microsoft/m365-spec-parser";
61+
62+
// Define parsing options
63+
const option: ParseOptions = {
64+
allowMissingId: false, // Allow missing IDs in the specification, default false
65+
allowSwagger: false, // Allow Swagger specifications, default false
66+
allowAPIKeyAuth: false, // Disallow API key authentication, default false
67+
allowMultipleParameters: false, // Disallow multiple parameters, default false
68+
allowOauth2: false, // Disallow OAuth2 authentication, default false
69+
};
70+
71+
// Create a new SpecParser instance with the given options
72+
const parser = new SpecParser("path/to/spec/file", option);
73+
74+
// Validate the specification
75+
const validateResult = await parser.validate();
76+
// If the specification is not valid, log the errors and warnings
77+
if (validateResult.status !== ValidationStatus.Valid) {
78+
console.log(validateResult.errors);
79+
console.log(validateResult.warnings);
80+
}
81+
82+
// List the operations in the specification
83+
const listResult = await parser.listSupportedAPIInfo();
84+
// Log each operation
85+
for (let i = 0; i < listResult.length; i++) {
86+
console.log(listResult[i]);
87+
}
88+
```
89+
90+
## Data Collection.
91+
92+
The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft's privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.
93+
94+
## Code of Conduct
95+
96+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
97+
98+
## Contributing
99+
100+
There are many ways in which you can participate in the project, for example:
101+
102+
- [Submit bugs and feature requests](https://github.com/OfficeDev/TeamsFx/issues), and help us verify as they are checked in
103+
- Review [source code changes](https://github.com/OfficeDev/TeamsFx/pulls)
104+
105+
If you are interested in fixing issues and contributing directly to the code base, please see the [Contributing Guide](./CONTRIBUTING.md).
106+
107+
## Reporting Security Issues
108+
109+
**Please do not report security vulnerabilities through public GitHub issues.**
110+
111+
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://msrc.microsoft.com/create-report).
112+
113+
If you prefer to submit without logging in, send email to [[email protected]](mailto:[email protected]). If possible, encrypt your message with our PGP key; please download it from the the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc).
114+
115+
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
116+
117+
## Trademarks
118+
119+
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.
120+
121+
## License
122+
123+
Copyright (c) Microsoft Corporation. All rights reserved.
124+
125+
Licensed under the [MIT](LICENSE.txt) license.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extensions": ["ts"],
3+
"spec": ["test/*.test.ts"],
4+
"require": ["ts-node/register"]
5+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
/*
5+
* Specify test tsconfig file for Windows
6+
*/
7+
process.env.TS_NODE_COMPILER_OPTIONS = '{ "module": "commonjs", "target": "es2016" }';
8+
process.env.TS_NODE_FILES = "true";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const webpack = require("webpack");
2+
module.exports = {
3+
mode: "development",
4+
devtool: "source-map",
5+
module: {
6+
rules: [
7+
{
8+
test: /\.tsx?$/,
9+
use: {
10+
loader: "ts-loader",
11+
options: {
12+
compilerOptions: {
13+
module: "commonjs",
14+
target: "es2017",
15+
downlevelIteration: true,
16+
resolveJsonModule: true,
17+
},
18+
},
19+
},
20+
},
21+
{
22+
test: /\.[tj]sx?$/,
23+
exclude: /node_modules/,
24+
use: "source-map-loader",
25+
enforce: "pre",
26+
},
27+
],
28+
},
29+
resolve: {
30+
modules: ["node_modules"],
31+
mainFields: ["browser", "module", "main"],
32+
extensions: [".js", ".ts"],
33+
symlinks: true,
34+
fallback: {
35+
url: require.resolve("url/"),
36+
http: require.resolve("stream-http"),
37+
https: require.resolve("https-browserify"),
38+
stream: require.resolve("stream-browserify"),
39+
buffer: require.resolve("buffer"),
40+
"process/browser": require.resolve("process/browser"),
41+
util: false,
42+
},
43+
},
44+
plugins: [
45+
new webpack.ProvidePlugin({
46+
process: "process/browser",
47+
}),
48+
new webpack.ProvidePlugin({
49+
Buffer: ["buffer", "Buffer"],
50+
}),
51+
],
52+
};

0 commit comments

Comments
 (0)