@sparted/shared-library 1.0.7 → 1.0.8-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +40 -0
- package/.eslintignore +1 -0
- package/.eslintrc.js +17 -0
- package/.gitignore +37 -0
- package/CHANGELOG.md +34 -0
- package/README.md +26 -0
- package/dist/github/commits/create-git-blob.d.ts +26 -0
- package/dist/github/commits/create-git-blob.js +42 -0
- package/dist/github/commits/create-git-blob.js.map +1 -0
- package/dist/github/commits/create-git-commit.d.ts +23 -0
- package/dist/github/commits/create-git-commit.js +39 -0
- package/dist/github/commits/create-git-commit.js.map +1 -0
- package/dist/github/commits/create-git-tree.d.ts +29 -0
- package/dist/github/commits/create-git-tree.js +47 -0
- package/dist/github/commits/create-git-tree.js.map +1 -0
- package/dist/github/commits/get-sha-last-commit-of-branch.d.ts +21 -0
- package/dist/github/commits/get-sha-last-commit-of-branch.js +35 -0
- package/dist/github/commits/get-sha-last-commit-of-branch.js.map +1 -0
- package/dist/github/commits/get-tree-by-commit-sha.d.ts +23 -0
- package/dist/github/commits/get-tree-by-commit-sha.js +37 -0
- package/dist/github/commits/get-tree-by-commit-sha.js.map +1 -0
- package/dist/github/commits/index.d.ts +6 -0
- package/dist/github/commits/index.js +20 -0
- package/dist/github/commits/run-new-commit-process.d.ts +27 -0
- package/dist/github/commits/run-new-commit-process.js +49 -0
- package/dist/github/commits/run-new-commit-process.js.map +1 -0
- package/dist/github/commits/utils.d.ts +1 -0
- package/dist/github/commits/utils.js +29 -0
- package/dist/github/commits/utils.js.map +1 -0
- package/dist/github/contents/get-file-content.d.ts +20 -0
- package/dist/github/contents/get-file-content.js +38 -0
- package/dist/github/contents/get-file-content.js.map +1 -0
- package/dist/github/github.d.ts +25 -0
- package/dist/github/github.js +53 -0
- package/dist/github/github.js.map +1 -0
- package/dist/github/index.d.ts +1 -0
- package/dist/github/index.js +10 -0
- package/dist/index.d.ts +15 -0
- package/{index.js → dist/index.js} +2 -0
- package/{index.js.map → dist/index.js.map} +1 -1
- package/dist/package.json +65 -0
- package/dist/string/index.js.map +1 -0
- package/dist/time/index.js.map +1 -0
- package/global.d.ts +2 -0
- package/package.json +7 -3
- package/rollup.config.ts +15 -0
- package/src/array/__tests__/array-to-map.test.ts +50 -0
- package/src/array/__tests__/get-values-from-keys.test.ts +14 -0
- package/src/array/array-to-map.ts +32 -0
- package/src/array/get-values-from-keys.ts +3 -0
- package/src/array/index.ts +2 -0
- package/src/cdn/__tests__/cdn.factory.test.ts +120 -0
- package/src/cdn/__tests__/utils.test.ts +456 -0
- package/src/cdn/cdn.factory.ts +63 -0
- package/src/cdn/index.ts +2 -0
- package/src/cdn/types/cdn-factory.type.ts +35 -0
- package/src/cdn/types/index.ts +1 -0
- package/src/cdn/utils/extract-info-from-url.util.ts +21 -0
- package/src/cdn/utils/get-cdn-from-s3-url.util.ts +20 -0
- package/src/cdn/utils/get-cdn-url.util.ts +26 -0
- package/src/formatting/__tests__/format-number.test.ts +11 -0
- package/src/formatting/format-number.ts +14 -0
- package/src/formatting/index.ts +1 -0
- package/src/fp/__tests__/run-while.test.ts +29 -0
- package/src/fp/index.ts +1 -0
- package/src/fp/run-while.ts +54 -0
- package/src/github/commits/create-git-blob.ts +57 -0
- package/src/github/commits/create-git-commit.ts +57 -0
- package/src/github/commits/create-git-tree.ts +68 -0
- package/src/github/commits/get-sha-last-commit-of-branch.ts +50 -0
- package/src/github/commits/get-tree-by-commit-sha.ts +52 -0
- package/src/github/commits/index.ts +6 -0
- package/src/github/commits/run-new-commit-process.ts +88 -0
- package/src/github/commits/utils.ts +31 -0
- package/src/github/contents/get-file-content.ts +50 -0
- package/src/github/github.ts +74 -0
- package/src/github/index.ts +1 -0
- package/src/http/__tests__/encode-search-object.test.ts +34 -0
- package/src/http/__tests__/fetch-with-timeout.test.ts +50 -0
- package/src/http/encode-search-object.ts +39 -0
- package/src/http/fetch-with-timeout.ts +24 -0
- package/src/http/index.ts +2 -0
- package/{index.d.ts → src/index.ts} +15 -14
- package/src/linked-list/__tests__/apply-diff-linked-list.test.ts +103 -0
- package/src/linked-list/__tests__/create-linked-list.test.ts +204 -0
- package/src/linked-list/__tests__/find-linked-list.test.ts +40 -0
- package/src/linked-list/__tests__/get-linked-list.test.ts +28 -0
- package/src/linked-list/__tests__/insert-linked-list.test.ts +186 -0
- package/src/linked-list/__tests__/linked-list-utils.test.ts +252 -0
- package/src/linked-list/__tests__/reduce-linked-list.test.ts +31 -0
- package/src/linked-list/__tests__/remove-linked-list.test.ts +129 -0
- package/src/linked-list/__tests__/update-linked-list.test.ts +53 -0
- package/src/linked-list/__tests__/while-linked-list.test.ts +58 -0
- package/src/linked-list/apply-diff-linked-list.ts +35 -0
- package/src/linked-list/create-linked-list.ts +79 -0
- package/src/linked-list/find-linked-list.ts +26 -0
- package/src/linked-list/get-linked-list.ts +9 -0
- package/src/linked-list/index.ts +2 -0
- package/src/linked-list/insert-linked-list.ts +50 -0
- package/src/linked-list/linked-list-utils.ts +143 -0
- package/src/linked-list/reduce-linked-list.ts +18 -0
- package/src/linked-list/remove-linked-list.ts +36 -0
- package/src/linked-list/type.ts +38 -0
- package/src/linked-list/update-linked-list.ts +29 -0
- package/src/linked-list/while-linked-list.ts +33 -0
- package/src/math/__tests__/clamp-number.test.ts +30 -0
- package/src/math/__tests__/interpolate.test.ts +28 -0
- package/src/math/__tests__/number-range.test.ts +59 -0
- package/src/math/__tests__/random-number.test.ts +11 -0
- package/src/math/clamp-number.ts +8 -0
- package/src/math/index.ts +4 -0
- package/src/math/interpolate.ts +18 -0
- package/src/math/number-range.ts +16 -0
- package/src/math/random-number.ts +10 -0
- package/src/object/__tests__/merge-deep.test.ts +56 -0
- package/src/object/index.ts +1 -0
- package/src/object/merge-deep.ts +27 -0
- package/src/parsing/__tests__/parse-boolean.test.ts +15 -0
- package/src/parsing/__tests__/parse-enum.test.ts +19 -0
- package/src/parsing/__tests__/parse-number.test.ts +20 -0
- package/src/parsing/index.ts +3 -0
- package/src/parsing/parse-boolean.ts +10 -0
- package/src/parsing/parse-enum.ts +16 -0
- package/src/parsing/parse-number.ts +12 -0
- package/src/predicates/__tests__/is-function.test.ts +11 -0
- package/src/predicates/__tests__/is-null.test.ts +11 -0
- package/src/predicates/__tests__/is-object.test.ts +11 -0
- package/src/predicates/__tests__/is-primitive.test.ts +42 -0
- package/src/predicates/__tests__/is-undefined.test.ts +11 -0
- package/src/predicates/__tests__/is-uuid.test.ts +13 -0
- package/src/predicates/index.ts +5 -0
- package/src/predicates/is-function.ts +3 -0
- package/src/predicates/is-null.ts +3 -0
- package/src/predicates/is-object.ts +3 -0
- package/src/predicates/is-primitive.ts +19 -0
- package/src/predicates/is-undefined.ts +3 -0
- package/src/predicates/is-uuid.ts +3 -0
- package/src/promise/__tests__/delay.test.ts +13 -0
- package/src/promise/__tests__/execute-or-resolve-default.test.ts +25 -0
- package/src/promise/__tests__/execute-with-delay.test.ts +24 -0
- package/src/promise/delay.ts +3 -0
- package/src/promise/execute-or-resolve-default.ts +14 -0
- package/src/promise/execute-with-delay.ts +6 -0
- package/src/promise/index.ts +3 -0
- package/src/string/__tests__/replace-in-template.test.ts +52 -0
- package/src/string/index.ts +1 -0
- package/src/string/replace-in-template.ts +45 -0
- package/src/time/__tests__/convert.test.ts +14 -0
- package/src/time/__tests__/format-minutes.test.ts +10 -0
- package/src/time/__tests__/get-time-diff-as-string.test.ts +128 -0
- package/src/time/constants.ts +4 -0
- package/src/time/convert.ts +5 -0
- package/src/time/format-minutes.ts +9 -0
- package/src/time/get-time-diff-as-string.ts +196 -0
- package/src/time/index.ts +4 -0
- package/src/types/function.ts +1 -0
- package/src/types/general.ts +8 -0
- package/src/types/index.ts +4 -0
- package/src/types/object.ts +33 -0
- package/src/types/search.ts +29 -0
- package/tsconfig.eslint.json +8 -0
- package/tsconfig.json +25 -0
- /package/{_virtual → dist/_virtual}/_tslib.js +0 -0
- /package/{_virtual → dist/_virtual}/_tslib.js.map +0 -0
- /package/{array → dist/array}/array-to-map.d.ts +0 -0
- /package/{array → dist/array}/array-to-map.js +0 -0
- /package/{array → dist/array}/array-to-map.js.map +0 -0
- /package/{array → dist/array}/get-values-from-keys.d.ts +0 -0
- /package/{array → dist/array}/get-values-from-keys.js +0 -0
- /package/{array → dist/array}/get-values-from-keys.js.map +0 -0
- /package/{array → dist/array}/index.d.ts +0 -0
- /package/{array → dist/array}/index.js +0 -0
- /package/{array → dist/array}/index.js.map +0 -0
- /package/{cdn → dist/cdn}/cdn.factory.d.ts +0 -0
- /package/{cdn → dist/cdn}/cdn.factory.js +0 -0
- /package/{cdn → dist/cdn}/cdn.factory.js.map +0 -0
- /package/{cdn → dist/cdn}/index.d.ts +0 -0
- /package/{cdn → dist/cdn}/index.js +0 -0
- /package/{cdn → dist/cdn}/index.js.map +0 -0
- /package/{cdn → dist/cdn}/types/cdn-factory.type.d.ts +0 -0
- /package/{cdn → dist/cdn}/types/index.d.ts +0 -0
- /package/{cdn → dist/cdn}/types/index.js +0 -0
- /package/{cdn → dist/cdn}/types/index.js.map +0 -0
- /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.d.ts +0 -0
- /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.js +0 -0
- /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.js.map +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.d.ts +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.js +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.js.map +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-url.util.d.ts +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-url.util.js +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-url.util.js.map +0 -0
- /package/{formatting → dist/formatting}/format-number.d.ts +0 -0
- /package/{formatting → dist/formatting}/format-number.js +0 -0
- /package/{formatting → dist/formatting}/format-number.js.map +0 -0
- /package/{formatting → dist/formatting}/index.d.ts +0 -0
- /package/{formatting → dist/formatting}/index.js +0 -0
- /package/{formatting → dist/formatting}/index.js.map +0 -0
- /package/{fp → dist/fp}/index.d.ts +0 -0
- /package/{fp → dist/fp}/index.js +0 -0
- /package/{fp → dist/fp}/index.js.map +0 -0
- /package/{fp → dist/fp}/run-while.d.ts +0 -0
- /package/{fp → dist/fp}/run-while.js +0 -0
- /package/{fp → dist/fp}/run-while.js.map +0 -0
- /package/{time → dist/github/commits}/index.js.map +0 -0
- /package/{linked-list → dist/github}/index.js.map +0 -0
- /package/{http → dist/http}/encode-search-object.d.ts +0 -0
- /package/{http → dist/http}/encode-search-object.js +0 -0
- /package/{http → dist/http}/encode-search-object.js.map +0 -0
- /package/{http → dist/http}/fetch-with-timeout.d.ts +0 -0
- /package/{http → dist/http}/fetch-with-timeout.js +0 -0
- /package/{http → dist/http}/fetch-with-timeout.js.map +0 -0
- /package/{http → dist/http}/index.d.ts +0 -0
- /package/{http → dist/http}/index.js +0 -0
- /package/{http → dist/http}/index.js.map +0 -0
- /package/{linked-list → dist/linked-list}/apply-diff-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/apply-diff-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/apply-diff-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/create-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/create-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/create-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/find-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/find-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/find-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/get-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/get-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/get-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/index.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/index.js +0 -0
- /package/{object → dist/linked-list}/index.js.map +0 -0
- /package/{linked-list → dist/linked-list}/insert-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/insert-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/insert-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/linked-list-utils.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/linked-list-utils.js +0 -0
- /package/{linked-list → dist/linked-list}/linked-list-utils.js.map +0 -0
- /package/{linked-list → dist/linked-list}/reduce-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/reduce-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/reduce-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/remove-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/remove-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/remove-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/type.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/update-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/update-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/update-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/while-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/while-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/while-linked-list.js.map +0 -0
- /package/{math → dist/math}/clamp-number.d.ts +0 -0
- /package/{math → dist/math}/clamp-number.js +0 -0
- /package/{math → dist/math}/clamp-number.js.map +0 -0
- /package/{math → dist/math}/index.d.ts +0 -0
- /package/{math → dist/math}/index.js +0 -0
- /package/{math → dist/math}/index.js.map +0 -0
- /package/{math → dist/math}/interpolate.d.ts +0 -0
- /package/{math → dist/math}/interpolate.js +0 -0
- /package/{math → dist/math}/interpolate.js.map +0 -0
- /package/{math → dist/math}/number-range.d.ts +0 -0
- /package/{math → dist/math}/number-range.js +0 -0
- /package/{math → dist/math}/number-range.js.map +0 -0
- /package/{math → dist/math}/random-number.d.ts +0 -0
- /package/{math → dist/math}/random-number.js +0 -0
- /package/{math → dist/math}/random-number.js.map +0 -0
- /package/{object → dist/object}/index.d.ts +0 -0
- /package/{object → dist/object}/index.js +0 -0
- /package/{string → dist/object}/index.js.map +0 -0
- /package/{object → dist/object}/merge-deep.d.ts +0 -0
- /package/{object → dist/object}/merge-deep.js +0 -0
- /package/{object → dist/object}/merge-deep.js.map +0 -0
- /package/{parsing → dist/parsing}/index.d.ts +0 -0
- /package/{parsing → dist/parsing}/index.js +0 -0
- /package/{parsing → dist/parsing}/index.js.map +0 -0
- /package/{parsing → dist/parsing}/parse-boolean.d.ts +0 -0
- /package/{parsing → dist/parsing}/parse-boolean.js +0 -0
- /package/{parsing → dist/parsing}/parse-boolean.js.map +0 -0
- /package/{parsing → dist/parsing}/parse-enum.d.ts +0 -0
- /package/{parsing → dist/parsing}/parse-enum.js +0 -0
- /package/{parsing → dist/parsing}/parse-enum.js.map +0 -0
- /package/{parsing → dist/parsing}/parse-number.d.ts +0 -0
- /package/{parsing → dist/parsing}/parse-number.js +0 -0
- /package/{parsing → dist/parsing}/parse-number.js.map +0 -0
- /package/{predicates → dist/predicates}/index.d.ts +0 -0
- /package/{predicates → dist/predicates}/index.js +0 -0
- /package/{predicates → dist/predicates}/index.js.map +0 -0
- /package/{predicates → dist/predicates}/is-function.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-function.js +0 -0
- /package/{predicates → dist/predicates}/is-function.js.map +0 -0
- /package/{predicates → dist/predicates}/is-null.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-null.js +0 -0
- /package/{predicates → dist/predicates}/is-null.js.map +0 -0
- /package/{predicates → dist/predicates}/is-object.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-object.js +0 -0
- /package/{predicates → dist/predicates}/is-object.js.map +0 -0
- /package/{predicates → dist/predicates}/is-primitive.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-primitive.js +0 -0
- /package/{predicates → dist/predicates}/is-primitive.js.map +0 -0
- /package/{predicates → dist/predicates}/is-undefined.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-undefined.js +0 -0
- /package/{predicates → dist/predicates}/is-undefined.js.map +0 -0
- /package/{predicates → dist/predicates}/is-uuid.d.ts +0 -0
- /package/{promise → dist/promise}/delay.d.ts +0 -0
- /package/{promise → dist/promise}/delay.js +0 -0
- /package/{promise → dist/promise}/delay.js.map +0 -0
- /package/{promise → dist/promise}/execute-or-resolve-default.d.ts +0 -0
- /package/{promise → dist/promise}/execute-or-resolve-default.js +0 -0
- /package/{promise → dist/promise}/execute-or-resolve-default.js.map +0 -0
- /package/{promise → dist/promise}/execute-with-delay.d.ts +0 -0
- /package/{promise → dist/promise}/execute-with-delay.js +0 -0
- /package/{promise → dist/promise}/execute-with-delay.js.map +0 -0
- /package/{promise → dist/promise}/index.d.ts +0 -0
- /package/{promise → dist/promise}/index.js +0 -0
- /package/{promise → dist/promise}/index.js.map +0 -0
- /package/{string → dist/string}/index.d.ts +0 -0
- /package/{string → dist/string}/index.js +0 -0
- /package/{string → dist/string}/replace-in-template.d.ts +0 -0
- /package/{string → dist/string}/replace-in-template.js +0 -0
- /package/{string → dist/string}/replace-in-template.js.map +0 -0
- /package/{time → dist/time}/constants.d.ts +0 -0
- /package/{time → dist/time}/constants.js +0 -0
- /package/{time → dist/time}/constants.js.map +0 -0
- /package/{time → dist/time}/convert.d.ts +0 -0
- /package/{time → dist/time}/convert.js +0 -0
- /package/{time → dist/time}/convert.js.map +0 -0
- /package/{time → dist/time}/format-minutes.d.ts +0 -0
- /package/{time → dist/time}/format-minutes.js +0 -0
- /package/{time → dist/time}/format-minutes.js.map +0 -0
- /package/{time → dist/time}/get-time-diff-as-string.d.ts +0 -0
- /package/{time → dist/time}/get-time-diff-as-string.js +0 -0
- /package/{time → dist/time}/get-time-diff-as-string.js.map +0 -0
- /package/{time → dist/time}/index.d.ts +0 -0
- /package/{time → dist/time}/index.js +0 -0
- /package/{types → dist/types}/function.d.ts +0 -0
- /package/{types → dist/types}/general.d.ts +0 -0
- /package/{types → dist/types}/index.d.ts +0 -0
- /package/{types → dist/types}/index.js +0 -0
- /package/{types → dist/types}/index.js.map +0 -0
- /package/{types → dist/types}/object.d.ts +0 -0
- /package/{types → dist/types}/search.d.ts +0 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type RunWhileConfig<T> = {
|
|
2
|
+
/**
|
|
3
|
+
* Return a boolean to keep running the callback or not.
|
|
4
|
+
*/
|
|
5
|
+
predicate: (current: T) => boolean,
|
|
6
|
+
/**
|
|
7
|
+
* The function to run while `predicate` returns `true`
|
|
8
|
+
*/
|
|
9
|
+
callback: (current: T, iteractionCount: number) => T,
|
|
10
|
+
/**
|
|
11
|
+
* `callback` will not be run again if `iterationCount` exceeds this value.
|
|
12
|
+
* Default to `Infinity`
|
|
13
|
+
*/
|
|
14
|
+
iterationLimit?: number,
|
|
15
|
+
/**
|
|
16
|
+
* The first value passed to the callback.
|
|
17
|
+
*/
|
|
18
|
+
initialValue: T,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type RunWhileFn = <T>(config: RunWhileConfig<T>) => {
|
|
22
|
+
/**
|
|
23
|
+
* The callback last result.
|
|
24
|
+
*/
|
|
25
|
+
result: T,
|
|
26
|
+
iterationCount: number,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Run the callback while the predicate return true. Throws an
|
|
31
|
+
* error if the iteration count exceeds the iteration limit.
|
|
32
|
+
*/
|
|
33
|
+
export const runWhile: RunWhileFn = ({ predicate, callback, initialValue, iterationLimit = Infinity }) => {
|
|
34
|
+
let counter = 0;
|
|
35
|
+
let currentValue = initialValue;
|
|
36
|
+
let shouldRun = predicate(currentValue);
|
|
37
|
+
|
|
38
|
+
while (shouldRun) {
|
|
39
|
+
if (counter >= iterationLimit) {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const nextValue = callback(currentValue, counter);
|
|
44
|
+
|
|
45
|
+
counter += 1;
|
|
46
|
+
shouldRun = predicate(nextValue);
|
|
47
|
+
currentValue = nextValue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
result: currentValue,
|
|
52
|
+
iterationCount: counter,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { handleError } from './utils';
|
|
5
|
+
|
|
6
|
+
export type CreateGitBlobArgs = {
|
|
7
|
+
content: string;
|
|
8
|
+
encoding: 'utf-8' | 'base64';
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type CreateGitBlobResponse = {
|
|
12
|
+
sha: string;
|
|
13
|
+
url: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const createGitBlobResponseSchema = z.object({
|
|
17
|
+
sha: z.string(),
|
|
18
|
+
url: z.string(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export type CreateGitBlob = (
|
|
22
|
+
args: CreateGitBlobArgs
|
|
23
|
+
) => Promise<CreateGitBlobResponse>;
|
|
24
|
+
|
|
25
|
+
export type CreateGitBlobFactory = (instance: AxiosInstance) => CreateGitBlob;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a git blob
|
|
29
|
+
*
|
|
30
|
+
* @param owner the owner of the repository
|
|
31
|
+
* @param repo the name of the repository
|
|
32
|
+
* @param token the github token
|
|
33
|
+
* @param content the content of the file
|
|
34
|
+
* @param encoding the encoding of the content. Default: utf-8
|
|
35
|
+
* @returns CreateGitBlobResponse
|
|
36
|
+
* @throws forbidden
|
|
37
|
+
* @throws resource-not-found
|
|
38
|
+
* @throws conflict
|
|
39
|
+
* @throws validation-failed-or-the-endpoint-has-been-spammed
|
|
40
|
+
*/
|
|
41
|
+
export const createGitBlobFactory: CreateGitBlobFactory = (instance) => async (args) => {
|
|
42
|
+
const { content, encoding = 'utf-8' } = args;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const { data } = await instance.post<CreateGitBlobResponse>(
|
|
46
|
+
'/git/blobs',
|
|
47
|
+
{
|
|
48
|
+
content,
|
|
49
|
+
encoding,
|
|
50
|
+
},
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return createGitBlobResponseSchema.parse(data);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw handleError(error);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { handleError } from './utils';
|
|
5
|
+
|
|
6
|
+
export type CreateGitCommitArgs = {
|
|
7
|
+
message: string;
|
|
8
|
+
treeSha: string;
|
|
9
|
+
parents: string[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type CreateGitCommitResponse = {
|
|
13
|
+
sha: string;
|
|
14
|
+
url: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const createGitCommitSchema = z.object({
|
|
18
|
+
sha: z.string(),
|
|
19
|
+
url: z.string(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export type CreateGitCommit = (
|
|
23
|
+
args: CreateGitCommitArgs
|
|
24
|
+
) => Promise<CreateGitCommitResponse>;
|
|
25
|
+
|
|
26
|
+
export type CreateGitCommitFactory = (
|
|
27
|
+
instance: AxiosInstance
|
|
28
|
+
) => CreateGitCommit;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create a git commit
|
|
32
|
+
* @param owner the owner of the repository
|
|
33
|
+
* @param repo the name of the repository
|
|
34
|
+
* @param token the github token
|
|
35
|
+
* @param message the commit message
|
|
36
|
+
* @param treeSha the sha of the tree
|
|
37
|
+
* @param parents the parents of the commit
|
|
38
|
+
* @returns CreateGitCommitResponse
|
|
39
|
+
*/
|
|
40
|
+
export const createGitCommitFactory: CreateGitCommitFactory = (instance) => async (args) => {
|
|
41
|
+
const { message, treeSha, parents } = args;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const { data } = await instance.post<CreateGitCommitResponse>(
|
|
45
|
+
'/git/commits',
|
|
46
|
+
{
|
|
47
|
+
message,
|
|
48
|
+
tree: treeSha,
|
|
49
|
+
parents,
|
|
50
|
+
},
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return createGitCommitSchema.parse(data);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw handleError(error);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { handleError } from './utils';
|
|
5
|
+
|
|
6
|
+
export type Blob = {
|
|
7
|
+
path: string;
|
|
8
|
+
sha: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type CreateGitTreeArgs = {
|
|
12
|
+
baseTreeSha: string;
|
|
13
|
+
blobs: Blob[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type CreateGitTreeResponse = {
|
|
17
|
+
sha: string;
|
|
18
|
+
url: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const createGitTreeSchema = z.object({
|
|
22
|
+
sha: z.string(),
|
|
23
|
+
url: z.string(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type CreateGitTree = (
|
|
27
|
+
args: CreateGitTreeArgs
|
|
28
|
+
) => Promise<CreateGitTreeResponse>;
|
|
29
|
+
|
|
30
|
+
export type CreateGitTreeFactory = (instance: AxiosInstance) => CreateGitTree;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create a git tree
|
|
34
|
+
*
|
|
35
|
+
* @param owner the owner of the repository
|
|
36
|
+
* @param repo the name of the repository
|
|
37
|
+
* @param token the github token
|
|
38
|
+
* @param baseTreeSha the sha of the base tree
|
|
39
|
+
* @param blobs the blobs to add to the tree
|
|
40
|
+
* @returns CreateGitTreeResponse
|
|
41
|
+
* @throws forbidden
|
|
42
|
+
* @throws resource-not-found
|
|
43
|
+
* @throws validation-failed-or-the-endpoint-has-been-spammed
|
|
44
|
+
*/
|
|
45
|
+
export const createGitTreeFactory: CreateGitTreeFactory = (instance) => async (args) => {
|
|
46
|
+
const { baseTreeSha, blobs } = args;
|
|
47
|
+
|
|
48
|
+
const tree = blobs.map((blob) => ({
|
|
49
|
+
path: blob.path,
|
|
50
|
+
mode: '100644',
|
|
51
|
+
type: 'blob',
|
|
52
|
+
sha: blob.sha,
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const { data } = await instance.post<CreateGitTreeResponse>(
|
|
57
|
+
'/git/trees',
|
|
58
|
+
{
|
|
59
|
+
base_tree: baseTreeSha,
|
|
60
|
+
tree,
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return createGitTreeSchema.parse(data);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
throw handleError(error);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { handleError } from './utils';
|
|
5
|
+
|
|
6
|
+
export type GetShaLastCommitOfBranchArgs = {
|
|
7
|
+
ref: `/heads/${string}`;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type GetShaLastCommitOfBranchResponse = {
|
|
11
|
+
sha: string;
|
|
12
|
+
url: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const getShaLastCommitOfBranchSchema = z.object({
|
|
16
|
+
sha: z.string(),
|
|
17
|
+
url: z.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export type GetShaLastCommitOfBranch = (
|
|
21
|
+
args: GetShaLastCommitOfBranchArgs
|
|
22
|
+
) => Promise<GetShaLastCommitOfBranchResponse>;
|
|
23
|
+
|
|
24
|
+
export type GetShaLastCommitOfBranchFactory = (
|
|
25
|
+
instance: AxiosInstance
|
|
26
|
+
) => GetShaLastCommitOfBranch;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get the sha of the last commit of a branch
|
|
30
|
+
*
|
|
31
|
+
* @param owner the owner of the repository
|
|
32
|
+
* @param repo the name of the repository
|
|
33
|
+
* @param ref the reference to a branch. Example: /heads/master
|
|
34
|
+
* @param token the github token
|
|
35
|
+
* @returns GetShaLastCommitOfBranchResponse
|
|
36
|
+
* @throws ref-not-found
|
|
37
|
+
*/
|
|
38
|
+
export const getShaLastCommitOfBranchFactory: GetShaLastCommitOfBranchFactory = (instance) => async (args) => {
|
|
39
|
+
const { ref } = args;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const { data } = await instance.get<{
|
|
43
|
+
object: GetShaLastCommitOfBranchResponse;
|
|
44
|
+
}>(`/git/refs/${ref}`);
|
|
45
|
+
|
|
46
|
+
return getShaLastCommitOfBranchSchema.parse(data.object);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw handleError(error);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { handleError } from './utils';
|
|
5
|
+
|
|
6
|
+
export type GetTreeFromCommitShaArgs = {
|
|
7
|
+
sha: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type GetTreeFromCommitShaResponse = {
|
|
11
|
+
url: string;
|
|
12
|
+
sha: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const getTreeFromCommitShaSchema = z.object({
|
|
16
|
+
url: z.string(),
|
|
17
|
+
sha: z.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export type GetTreeByCommitSha = (
|
|
21
|
+
args: GetTreeFromCommitShaArgs
|
|
22
|
+
) => Promise<GetTreeFromCommitShaResponse>;
|
|
23
|
+
|
|
24
|
+
export type GetTreeByCommitShaFactory = (
|
|
25
|
+
instance: AxiosInstance
|
|
26
|
+
) => GetTreeByCommitSha;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get the tree of a commit
|
|
30
|
+
*
|
|
31
|
+
* @param owner the owner of the repository
|
|
32
|
+
* @param repo the name of the repository
|
|
33
|
+
* @param sha the sha of the commit
|
|
34
|
+
* @param token the github token
|
|
35
|
+
* @returns GetTreeFromCommitShaResponse
|
|
36
|
+
* @throws sha-not-found
|
|
37
|
+
* @throws validation-failed-or-the-endpoint-has-been-spammed
|
|
38
|
+
* @throws internal-server-error
|
|
39
|
+
*/
|
|
40
|
+
export const getTreeByCommitShaFactory: GetTreeByCommitShaFactory = (instance) => async (args) => {
|
|
41
|
+
const { sha } = args;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const { data } = await instance.get<{
|
|
45
|
+
tree: GetTreeFromCommitShaResponse;
|
|
46
|
+
}>(`/git/commits/${sha}`);
|
|
47
|
+
|
|
48
|
+
return getTreeFromCommitShaSchema.parse(data.tree);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw handleError(error);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { CreateGitBlob } from './create-git-blob';
|
|
2
|
+
import type { CreateGitCommit } from './create-git-commit';
|
|
3
|
+
import type { CreateGitTree } from './create-git-tree';
|
|
4
|
+
import type { GetShaLastCommitOfBranch } from './get-sha-last-commit-of-branch';
|
|
5
|
+
import type { GetTreeByCommitSha } from './get-tree-by-commit-sha';
|
|
6
|
+
|
|
7
|
+
export type VersionedFile = {
|
|
8
|
+
path: string;
|
|
9
|
+
content: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type RunNewCommitProcessArgs = {
|
|
13
|
+
message: string;
|
|
14
|
+
ref: `/heads/${string}`;
|
|
15
|
+
files: VersionedFile[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type RunNewCommitProcessResponse = {
|
|
19
|
+
sha: string;
|
|
20
|
+
url: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type RunNewCommitProcess = (
|
|
24
|
+
args: RunNewCommitProcessArgs,
|
|
25
|
+
updateState?: (state: string) => void
|
|
26
|
+
) => Promise<RunNewCommitProcessResponse>;
|
|
27
|
+
|
|
28
|
+
export type RunNewCommitProcessFactory = (args: {
|
|
29
|
+
createGitBlob: CreateGitBlob;
|
|
30
|
+
createGitCommit: CreateGitCommit;
|
|
31
|
+
createGitTree: CreateGitTree;
|
|
32
|
+
getShaLastCommitOfBranch: GetShaLastCommitOfBranch;
|
|
33
|
+
getTreeByCommitSha: GetTreeByCommitSha;
|
|
34
|
+
}) => RunNewCommitProcess;
|
|
35
|
+
|
|
36
|
+
export const runNewCommitProcessFactory: RunNewCommitProcessFactory = (args) => async (
|
|
37
|
+
runNewCommitProcessArgs,
|
|
38
|
+
updateState = () => {},
|
|
39
|
+
) => {
|
|
40
|
+
const { createGitBlob, createGitCommit, createGitTree, getShaLastCommitOfBranch, getTreeByCommitSha } = args;
|
|
41
|
+
|
|
42
|
+
const { message, ref, files } = runNewCommitProcessArgs;
|
|
43
|
+
|
|
44
|
+
updateState('retrieving-sha-last-commit-of-branch');
|
|
45
|
+
const { sha: shaLastCommit } = await getShaLastCommitOfBranch({
|
|
46
|
+
ref,
|
|
47
|
+
});
|
|
48
|
+
updateState('retrieved-sha-last-commit-of-branch');
|
|
49
|
+
|
|
50
|
+
updateState('creating-blobs-from-file');
|
|
51
|
+
const blobs = await Promise.all(
|
|
52
|
+
files.map(async (file) => {
|
|
53
|
+
const blob = await createGitBlob({
|
|
54
|
+
content: file.content,
|
|
55
|
+
encoding: 'utf-8',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
path: file.path,
|
|
60
|
+
sha: blob.sha,
|
|
61
|
+
};
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
updateState('created-blobs-from-file');
|
|
65
|
+
|
|
66
|
+
updateState('retrieving-branch-current-tree');
|
|
67
|
+
const { sha: lastCommitTreeSha } = await getTreeByCommitSha({
|
|
68
|
+
sha: shaLastCommit,
|
|
69
|
+
});
|
|
70
|
+
updateState('retrieved-branch-current-tree');
|
|
71
|
+
|
|
72
|
+
updateState('creating-new-tree-with-changes');
|
|
73
|
+
const tree = await createGitTree({
|
|
74
|
+
baseTreeSha: lastCommitTreeSha,
|
|
75
|
+
blobs,
|
|
76
|
+
});
|
|
77
|
+
updateState('created-new-tree-with-changes');
|
|
78
|
+
|
|
79
|
+
updateState('creating-new-commit');
|
|
80
|
+
const commit = await createGitCommit({
|
|
81
|
+
message,
|
|
82
|
+
treeSha: tree.sha,
|
|
83
|
+
parents: [shaLastCommit],
|
|
84
|
+
});
|
|
85
|
+
updateState('created-new-commit');
|
|
86
|
+
|
|
87
|
+
return commit;
|
|
88
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AxiosResponse } from 'axios';
|
|
2
|
+
|
|
3
|
+
export const handleError = (error: unknown): Error => {
|
|
4
|
+
const { response } = error as any;
|
|
5
|
+
|
|
6
|
+
if (response) {
|
|
7
|
+
const { status } = response as AxiosResponse;
|
|
8
|
+
|
|
9
|
+
if (status === 403) {
|
|
10
|
+
return new Error('forbidden');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (status === 404) {
|
|
14
|
+
return new Error('resource-not-found');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (status === 409) {
|
|
18
|
+
return new Error('conflict');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (status === 422) {
|
|
22
|
+
return new Error('validation-failed-or-the-endpoint-has-been-spammed');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (status === 500) {
|
|
26
|
+
return new Error('internal-server-error');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return error as Error;
|
|
31
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
import { handleError } from '../commits/utils';
|
|
4
|
+
|
|
5
|
+
export type GetFileContentArgs = {
|
|
6
|
+
path: string;
|
|
7
|
+
branch: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type ApiResponse = {
|
|
11
|
+
encoding: 'base64' | 'utf-8';
|
|
12
|
+
content: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type GetFileContentResponse = string;
|
|
16
|
+
|
|
17
|
+
export type GetFileContent = (args: GetFileContentArgs) => Promise<GetFileContentResponse>;
|
|
18
|
+
|
|
19
|
+
export type GetFileContentFactory = (instance: AxiosInstance) => GetFileContent;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get the content of a file
|
|
23
|
+
*
|
|
24
|
+
* @param path the path of the file
|
|
25
|
+
* @param branch the branch of the file
|
|
26
|
+
* @returns GetFileContentResponse
|
|
27
|
+
* @throws validation-failed-or-the-endpoint-has-been-spammed
|
|
28
|
+
* @throws internal-server-error
|
|
29
|
+
* @throws forbidden
|
|
30
|
+
* @throws resource-not-found
|
|
31
|
+
*/
|
|
32
|
+
export const getFileContentFactory: GetFileContentFactory = (instance) => async (args) => {
|
|
33
|
+
const { path, branch } = args;
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const { data } = await instance.get<ApiResponse>(`/contents/${path}`, {
|
|
37
|
+
params: {
|
|
38
|
+
ref: branch,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (data.encoding === 'base64') {
|
|
43
|
+
return Buffer.from(data.content, 'base64').toString('utf-8');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return data.content;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw handleError(error);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createGitBlobFactory,
|
|
5
|
+
createGitCommitFactory,
|
|
6
|
+
createGitTreeFactory,
|
|
7
|
+
getShaLastCommitOfBranchFactory,
|
|
8
|
+
getTreeByCommitShaFactory,
|
|
9
|
+
runNewCommitProcessFactory,
|
|
10
|
+
} from './commits';
|
|
11
|
+
import type {
|
|
12
|
+
CreateGitTree,
|
|
13
|
+
CreateGitBlob,
|
|
14
|
+
CreateGitCommit,
|
|
15
|
+
GetTreeByCommitSha,
|
|
16
|
+
GetShaLastCommitOfBranch,
|
|
17
|
+
RunNewCommitProcess,
|
|
18
|
+
} from './commits';
|
|
19
|
+
import { GetFileContent, getFileContentFactory } from './contents/get-file-content';
|
|
20
|
+
|
|
21
|
+
export type GithubSdkArgs = {
|
|
22
|
+
owner: string;
|
|
23
|
+
repo: string;
|
|
24
|
+
token: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type GithubSdkInstance = {
|
|
28
|
+
createGitBlob: CreateGitBlob;
|
|
29
|
+
createGitCommit: CreateGitCommit;
|
|
30
|
+
createGitTree: CreateGitTree;
|
|
31
|
+
getShaLastCommitOfBranch: GetShaLastCommitOfBranch;
|
|
32
|
+
getTreeByCommitSha: GetTreeByCommitSha;
|
|
33
|
+
runNewCommitProcess: RunNewCommitProcess;
|
|
34
|
+
getFileContent: GetFileContent;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const githubSdk = (args: GithubSdkArgs) => {
|
|
38
|
+
const { owner, repo, token } = args;
|
|
39
|
+
|
|
40
|
+
const instance = axios.create({
|
|
41
|
+
baseURL: `https://api.github.com/repos/${owner}/${repo}`,
|
|
42
|
+
headers: {
|
|
43
|
+
Accept: 'application/vnd.github+json',
|
|
44
|
+
Authorization: `Bearer ${token}`,
|
|
45
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const createGitBlob = createGitBlobFactory(instance);
|
|
50
|
+
const createGitCommit = createGitCommitFactory(instance);
|
|
51
|
+
const createGitTree = createGitTreeFactory(instance);
|
|
52
|
+
const getShaLastCommitOfBranch = getShaLastCommitOfBranchFactory(instance);
|
|
53
|
+
const getTreeByCommitSha = getTreeByCommitShaFactory(instance);
|
|
54
|
+
|
|
55
|
+
const getFileContent = getFileContentFactory(instance);
|
|
56
|
+
|
|
57
|
+
const runNewCommitProcess = runNewCommitProcessFactory({
|
|
58
|
+
createGitBlob,
|
|
59
|
+
createGitCommit,
|
|
60
|
+
createGitTree,
|
|
61
|
+
getShaLastCommitOfBranch,
|
|
62
|
+
getTreeByCommitSha,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
createGitBlob,
|
|
67
|
+
createGitCommit,
|
|
68
|
+
createGitTree,
|
|
69
|
+
getShaLastCommitOfBranch,
|
|
70
|
+
getTreeByCommitSha,
|
|
71
|
+
runNewCommitProcess,
|
|
72
|
+
getFileContent,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './github';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { encodeSearchObject } from '../encode-search-object';
|
|
2
|
+
|
|
3
|
+
describe('encodeSearchObject', () => {
|
|
4
|
+
it('should not encode sort and query if undefined', () => {
|
|
5
|
+
const url = encodeSearchObject({
|
|
6
|
+
limit: 10,
|
|
7
|
+
offset: 15,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
expect(url).toStrictEqual('offset=15&limit=10');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should encode sort and query twice', () => {
|
|
14
|
+
const url = encodeSearchObject({
|
|
15
|
+
limit: 10,
|
|
16
|
+
offset: 0,
|
|
17
|
+
sort: {
|
|
18
|
+
name: 'asc',
|
|
19
|
+
editionDate: 'desc',
|
|
20
|
+
},
|
|
21
|
+
query: {
|
|
22
|
+
name: 'The name of something',
|
|
23
|
+
status: 'published',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(url).toStrictEqual([
|
|
28
|
+
'offset=0',
|
|
29
|
+
'limit=10',
|
|
30
|
+
'sort=name%3Aasc%2BeditionDate%3Adesc',
|
|
31
|
+
'query=name%3AThe%2520name%2520of%2520something%2Bstatus%3Apublished',
|
|
32
|
+
].join('&'));
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/* eslint-disable import/first -- https://github.com/jefflau/jest-fetch-mock#to-setup-for-an-individual-test */
|
|
2
|
+
|
|
3
|
+
import fetchMock, { enableFetchMocks } from 'jest-fetch-mock';
|
|
4
|
+
|
|
5
|
+
// must appear before any other imports
|
|
6
|
+
enableFetchMocks();
|
|
7
|
+
|
|
8
|
+
import { fetchWithTimeout } from '../fetch-with-timeout';
|
|
9
|
+
|
|
10
|
+
describe('fetchWithTimeout', () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
fetchMock.resetMocks();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should call fetch with the same parameters', async () => {
|
|
16
|
+
const url = 'http://some/url';
|
|
17
|
+
const init = {
|
|
18
|
+
headers: {
|
|
19
|
+
some: 'header',
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
fetchMock.mockResponseOnce('mock response');
|
|
24
|
+
const response = await fetchWithTimeout(url, init);
|
|
25
|
+
|
|
26
|
+
expect(fetch as any).toHaveBeenNthCalledWith(1, url, expect.objectContaining(init));
|
|
27
|
+
expect(await response.text()).toBe('mock response');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should abort the request if timeout has elapsed', async () => {
|
|
31
|
+
jest.useFakeTimers();
|
|
32
|
+
const TIMEOUT = 3000;
|
|
33
|
+
const REQUEST_TIMEOUT = TIMEOUT * 2;
|
|
34
|
+
|
|
35
|
+
// This request will take too long
|
|
36
|
+
fetchMock.mockResponseOnce(async () => {
|
|
37
|
+
jest.advanceTimersByTime(REQUEST_TIMEOUT);
|
|
38
|
+
return 'mock response';
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// https://github.com/jefflau/jest-fetch-mock/issues/159
|
|
42
|
+
(global as any).DOMException = Error;
|
|
43
|
+
|
|
44
|
+
const responsePromise = fetchWithTimeout('http://some/url', { timeout: TIMEOUT });
|
|
45
|
+
|
|
46
|
+
jest.advanceTimersByTime(TIMEOUT + 100);
|
|
47
|
+
|
|
48
|
+
await expect(responsePromise).rejects.toThrow('The operation was aborted. ');
|
|
49
|
+
});
|
|
50
|
+
});
|